We’ll be covering the following topics in this tutorial:
Redirecting exceptions using throws
Recall that the code capable of throwing an exception is kept in the try block and the exceptions are caught in the catch block. When there is no appropriate catch block to handle the (checked) exception that was thrown by an object, the compiler does not compile the program. To overcome this, Java allows the programmer to redirect exceptions that have been raised up the call stack, by using the keyword throws. Thus, an exception thrown by a method can be handled either in the method itself or passed to a different method in the call stack.
To pass exceptions up to the call stack, the method must be declared with a throws clause.
All the exceptions thrown by a method can be declared with a single throws clause; the clause consists of the keyword throws followed by a comma-separated list of all the exceptions, as shown below:
Void RedirectExMethod( ) throws Exception_A, Exception B, Exception C { }
Program illustrates two methods redirecting an exception Exception from the method
throwing it, that is, ConvertAndDivide, to the calling method, main.
Program Redirecting exceptions.
public class Divide { public static void main(String[] args) { System.out.println("\n Program Execution starts here\n"); try { convertAndDivide ( args[0],args[1]); } catch(Exception e) { System.out.println (e.getMessage () +"\n"); e.printStackTrace (); } System.out.println("\n Program Execution Completes here"); } static void convertAndDivide (String s 1,String s2) throwsException { int a, b, c; a = Integer.parselnt (s1); b = Integer.parselnt (s2); c = divide (a, b); System.out.println( a + "/" + b + "=" + C ); } static int divide(int x, int y) throws Exception { if (y==0) { throw new Exception("Second Argument is Zero ..."); } return x/y; } }
The output of Program is the following:
C:\javatest>java Divide 4 0
Program Execution starts here
Second Argument is Zero …
Program Execution Completes here
C:\javatest>java Divide 4 a
Program Execution starts here a
java.lang.NumberFormatException: a at java.lang.lnteger.parse Int (1nteger.java:426) at java.lang.lnteger.parsel Int (1ntege r.java:476) at Divide.convertAndDivide (Divide.java:26) at Divide.main (Divide,java:9)
Program Execution Completes here
C:\javatest
The method getMessage() in Program just prints the message that is given at the time of the throw statement. If no message is given, then the data value that is responsible for the exception is shown; printStackTrace() prints the all the names of the methods that are called in generating the exception. The method names are printed in the reverse order of their call.
While defining exception handlers, it is instructive to take into account the scope of a method. The scope of the exception-handling mechanism is not limited to the exceptions that can be thrown by the code written into the method; it extends to the methods that called the method in which the exception is thrown. That is, the scope also includes exceptions thrown by methods called by that method and so on.
Rethrowing an exception
An exception that is caught in the try block can be thrown once again and can be handled. The try block just above the rethrow statement will catch the rethrown object. If there is no try block just above the rethrow statement then the method containing the rethrow statement handles it. To propagate an exception, the catch block can choose to rethrow the exception by using the throw statement. Note that there is no special syntax for rethrowing. Program illustrates how an exception can be rethrown .
Program Rethrowing exceptions.
public class Divide { public static void main(String[] args) { int a, b, c; try { a = Integer.parselnt (args [0]); b = Integer.parselnt (args [l]); try { c = a/b; System.out.println ( a + "I" + b + "=" + c); } catch (ArithmeticException e) { System.out.println ("Second Argument Should not be Zero"); System.out.println ("Rethrowing the object again"); throw e; } } { System.out.println("Arguments passed should be valid Numbers"); } catch(ArraylndexOutOfBoundsException e) { System.out.println(Pass Proper Arguments"); } System.out.println("\n Program Execution Completes here"); } }
Often first-time readers may get confused about the use of the three keywords: throw, throws and Throwable. It is therefore useful to dwell on the three concepts together in order to clarify their meaning.
Throwable is a class. Though the Throwable class is derived from the java.lang.Object class in the Java class library, Throwable is the super-class of all classes that handle exceptions.
The keyword throw is a statement that throws an exception. Note that an exception can be thrown either by the throw statement or when an error occurs during the execution of any other statement.
The keyword throws is a clause specified in the method definition which indicates that the method throws the exceptions mentioned after the keyword throws, which are handled in the called methods.