Java

Java – Throws Keyword In Exception Handling

To handle checked exceptions, use the term throws. Checked and unchecked exceptions exist, as we learnt in the last essay. Otherwise, the program won’t build when a checked exception occurs during compilation. Unchecked exceptions (such as Runtime) are not checked during compilation, on the other hand. Checked exceptions are handled using the throws keyword. Using the throws keyword, many exceptions may be declared.

The throws keyword vs try-catch in Java

You may be asking why we need the throws keyword because Java’s try-catch block can handle exceptions. That is a good question, to be sure. We already know that try-catch blocks may be used to manage exceptions.

Although the throws keyword performs the same function as try-catch, there are several situations in which you might prefer throws to try-catch. For instance, if the method myMethod() contains statements that may result in either an ArithmeticException or a NullPointerException, you could utilize try-catch as illustrated in the example below:

public void myMethod()
{
  try {
    // Statements that might throw an exception 
  }
  catch (ArithmeticException e) {
    // Exception handling statements
  }
  catch (NullPointerException e) {
    // Exception handling statements
  }
}

However, if you have several methods that potentially lead to exceptions, it would be laborious to write try-catch statements for each one. The code will get bloated and become harder to read.

Using throws in a manner that declares exceptions in the method signature and uses try-catch to handle exceptions when the method is being called is one technique to solve this issue.

The fact that you will be compelled to handle exceptions when using this function and all exceptions specified using throws must be handled or you will encounter compilation errors is another benefit of utilizing this technique.

public void myMethod() throws ArithmeticException, NullPointerException
{
  // Statements that might throw an exception 
}

public static void main(String args[]) { 
  try {
    myMethod();
  }
  catch (ArithmeticException e) {
    // Exception handling statements
  }
  catch (NullPointerException e) {
    // Exception handling statements
  }
}

Example of throws Keyword

Two checked exceptions are thrown by the method myMethod() in this example, thus we have stated these exceptions using the throws keyword in the method signature. The software will raise a compilation error if we do not specify these exceptions.

import java.io.*;
class ThrowExample { 
  void myMethod(int num)throws IOException, ClassNotFoundException{ 
     if(num==1)
        throw new IOException("IOException Occurred");
     else
        throw new ClassNotFoundException("ClassNotFoundException");
  } 
} 

public class Example1{ 
  public static void main(String args[]){ 
   try{ 
     ThrowExample obj=new ThrowExample(); 
     obj.myMethod(1); 
   }catch(Exception ex){
     System.out.println(ex);
    } 
  }
}

Output:

java.io.IOException: IOException Occurred

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button