Keyword throws in Java
In the program, if there is any chance that the check exception will be thrown then we should handle this exception compulsory. If in case, check exceptions handling does not occur than the compiler will give compile time error saying “unreported exceptions xxx must be caught or declare to be thrown”. For example,
public class ExampleOne { public static void main(String []args) { Thread.sleep(1000); //a statement which throws checked exception } }
In the above code, statement we will get compile time error as unreported exception java.lang.InterruptedException must be caught. This error says that the statement is throwing an exceptions and we should should catch it. One can solve this using 2 ways as follows:
- By using try catch: Using try and catch block above code we can rewritte it as
public class ExampleOne { public static void main(String []args) { try { Thread.sleep(1000); } catch(InterruptedException ie) { } } }
2) By using throws keyword: We can use throws keyword to delegate the responsibility of exception_handling to the caller method. After using the throws keyword program will be as
public class ExampleOne { public static void main(String []args) throws InterruptedException { Thread.sleep(1000); } }
Main purpose of using throws keyword is to catch exceptions without using try and catch blocks. We can also use throws for both for check as well as uncheck exceptions, but good practice recommend to catch only check exceptions.
Consider the following example,
public class ExampleTwo { public static void main(String []args) throws InterruptedException { doWork(); } public static doWork() throws InterruptedException { doMoreWork(); } public static doMoreWork() throws InterruptedException { Thread.sleep(1000); } }
In the above program, if we will remove any of the throw keyword from any method, then we will get the compile-time-error as all 3 throws keyword are compulsory.
Note- We can only use Throws keyword for Throwable types, using it for other than Throwable we will get compile time error as incompatible types. For example,
public class ExampleThree { public static void main(String []args) throws ExampleThree { //this is invalid } }
The above code is invalid. Valid version of the above code will be as follows:
public class ExampleThree extends Exception { public static void main(String []args) throws ExampleThree { //this is valid } }
Following are some cases which are some possible use of throws keyword:
Case 1:
public class ExampleFour { public static void main(String []args) { throw new Exception(); } }
Since in the above code the exception is check, its handling should compulsory be either by try and catch or using throws keyword.
Case 2:
public class ExampleFive { public static void main(String []args) { throw new Error(); } }
Since the Error is uncheck, it does not require to handle it using either try and catch or throws.
Case 3: If there are no chances of throwing any exception, then we cannot define catch blocks for that exception, otherwise we will get compile time error. But this rule is applicable for only fully check exceptions. For example,
try { System.out.println("Hello World"); } //<strong>Case 1</strong> catch(ArithmeticException ae) //valid { } //<strong>Case 2</strong> catch(IOException ioe) //invalid { } //<strong>Case 3</strong> catch(InterruptedException ie) //invalid { } //<strong>Case 4</strong> catch(Error e) //valid { } //<strong>Case 5</strong> catch(Exception e) //valid { }
In the above invalid cases, we get the compile time error: “Exception xxx is never thrown in body of corresponding try statement”. In the rest valid cases program will terminate normally.