throws :
- In our program, if there is any chance of raising checked exception then it is compulsory that we should handle it otherwise we will get compile time error saying unreported exception which must be caught or declared to be thrown.
- 'throws' is the keyword which gives an indication to the calling function to keep the called function under try and catch blocks.
Syntax: <Return type> method name(number of parameters if any) throws type of exception 1,type of exception 2,………type of exception n;
Example:
class Example { public static void main (String[ ] args) { Thread.sleep(2000); } }
Output:
Compile time error : unreported exception InterruptedException; must be caught or declared to be thrown
This problem can be handled using the following ways :
- By using try-catch :-
class Example { public static void main (String[ ] args) { try { Thread.sleep(2000); } catch(InterruptedException ie) { } } }
- By using throws :-
class Example { public static void main (String[ ] args) throws InterruptedException { Thread.sleep(2000); } }
- Hence, the main purpose of throws keyword is to delegate the responsibility of exception handing to the caller method in the case of checked exception to convince compiler.
- In the case of unchecked exception, it is not required to use throws keyword.
class Example { public static void main (String[ ] args) throws InterruptedException { algo(); } public static void algo() throws InterruptedException { valley(); } public static void valley() throws InterruptedException { Thread.sleep(2000); } }//valid
- In the above program if we are removing any throws keyword then the code won't be compile.
- We can use throws keyword only for Throwable types otherwise we will get compile time error saying incompatible types.
class Example { public static void main (String[ ] args) throws Example { } }//Compile time error : incompatible types required : Throwable found : Example
class Example extends Exception { public static void main (String[ ] args) throws Example { } }//valid
class Example { public static void main (String[ ] args) { throw new Exception(); } }//Compile time error : unreported exception Exception; must be caught or declared to be thrown //As Exception is checked it is compulsory to handle it either by try-catch block or by throws keyword.
class Example { public static void main (String[ ] args) { throw new Error(); } }//Runtime Exception : Exception in thread "main" java.lang.Error //As Error is unchecked it is not required to handle it by try-catch block or by throws keyword.
- If there is no chance of raising an exception then we can't define catch block for that exception otherwise we will get compile time error but this rule is applicable for fully checked exception.
Example 1:
class Example { public static void main (String[ ] args) { try { System.out.println("Algo"); } catch(Exception e) { } } }
Output:
Algo
Example 2:
class Example { public static void main (String[ ] args) { try { System.out.println("Algo"); } catch(ArithmeticException e) { } } }
Output:
Algo
Example 3:
class Example { public static void main (String[ ] args) { try { System.out.println("Algo"); } catch(InterruptedException e) { } } }
Output:
Compile time error : exception InterruptedException is never thrown in body of corresponding try statement
Number of ways to find details of the exception:
In Java there are three ways to find the details of the exception. They are : using an object of java.lang.Exception class, using public void printStackTrace method and using public string getMessage method.
Using an object of java.lang.Exception : An object of Exception class prints the name of the exception and nature of the message.
Syntax : Name of exception : nature of message
Example:
try { int x = Integer.parseInt("10x"); } catch(Exception e) { System.out.println(e); }// Runtime exception : java.lang.NumberFormatException: For input string: "10x"
Using printStackTrace method : This is the method which is defined in java.lang.Throwable class and it is inherited into java.lang.Error class and java.lang.Exception class. This method will display name of the exception, nature of the message and line number where the exception has taken place.
Syntax : Name of exception : nature of message Stack trace
Example:
try { ......; int x = 10/0; ......; } catch (Exception e) { e.printStackTrace(); } // java.lang.ArithmeticException : / by zero at line no : 4
Using getMessage method : This is also a method which is defined in java.lang.Throwable class and it is inherited into both Error and Exception classes. This method will display only nature of message.
Syntax : Nature of message
Example:
try { ......; int x = 10/0; ......; } catch (Exception e) { System.out.println (e.getMessage ()); } // / by zero
Note :
By default exception handler internally uses printStackTrace().