The Java throws keyword is used to declare an exception. It gives an information to the caller method that there is a chance to occur an exception in the execution of the method.
The main objective of throws keyword is to delegate the responsibility of exception handling to the caller method
- We can declare both checked and unchecked exception and Errors
- throws doesn’t really throw an exception, it is just a declaration of exception to the caller
- We can declare multiple exceptions in any order with one throws keyword
- We can declare mix of checked and unchecked exception in any order
- In method declaration we are allowed to use only one throws keyword
- throws is mandatory in method signature if method throw checked exception otherwise we will get compilation error
- throws is optional in method signature if method throw unchecked exception
- throws should declare only throwable types otherwise we will get compile time error saying incompatible types
Syntax of java throws
1 2 3 |
return_type method_name() throws exception_class_name1, exception_class_nam2, ...{ //method code } |
Example 01: throws keyword – declare unchecked exception
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.vidvaan.corejava.exception22.throwskeyword; // throws keyword - declare unchecked exception public class Example01DeclareUncheckedException { // declaring that there would be chance of IllegalArgumentException if some one // calls this method public void doStuff() throws IllegalArgumentException { throw new IllegalArgumentException("Something wrong !!!"); // exception propagated to main() method } public static void main(String args[]) { // // if not handled in main() method exception propagated to // defaultExceptionHandler System.out.println("main() begin"); Example01DeclareUncheckedException obj = new Example01DeclareUncheckedException(); obj.doStuff(); System.out.println("main() end"); } } |
Output :
main() begin
Exception in thread “main” java.lang.IllegalArgumentException: Something wrong !!!
at com.vidvaan.corejava.exception22.throwskeyword.Example01DeclareUncheckedException.doStuff(Example01DeclareUncheckedException.java:9)
at com.vidvaan.corejava.exception22.throwskeyword.Example01DeclareUncheckedException.main(Example01DeclareUncheckedException.java:18)
In the above example, doStuff() method declares unchecked IllegalArgumentException. As it is unchecked exception the caller method main() not mandatory to handle it or declare it. As it is not handled any where in the call stack, program got terminated.
Example 02: throws keyword – caller handle unchecked exception
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.vidvaan.corejava.exception22.throwskeyword; // throws keyword - caller handled unchecked exception public class Example02CallerHandleUncheckedException { // declaring that there would be chance of IllegalArgumentException if some one // calls this method public void doStuff() throws IllegalArgumentException { throw new IllegalArgumentException("Something wrong !!!"); // exception propagated to main() method } public static void main(String args[]) { // // if not handled in main() method exception propagated to // defaultExceptionHandler System.out.println("main() begin"); Example02CallerHandleUncheckedException obj = new Example02CallerHandleUncheckedException(); try { obj.doStuff(); } catch (IllegalArgumentException e) { e.printStackTrace(); } System.out.println("main() end"); } } |
Output:
main() begin
java.lang.IllegalArgumentException: Something wrong !!!
at com.vidvaan.corejava.exception22.throwskeyword.Example02CallerHandleUncheckedException.doStuff(Example02CallerHandleUncheckedException.java:9)
at com.vidvaan.corejava.exception22.throwskeyword.Example02CallerHandleUncheckedException.main(Example02CallerHandleUncheckedException.java:19)
main() end
In the above example, doStuff() method declares unchecked IllegalArgumentException. Here caller main() method has coded to handle IllegalArgumentException. As exception handled, program successfully executed without abnormal termination.
Example 03: throws keyword – caller too declare unchecked exception
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.vidvaan.corejava.exception22.throwskeyword; //throws keyword - caller declare unchecked exception public class Example03CallerDeclareUncheckedException { // declaring that there would be chance of IllegalArgumentException if some one // calls this method public void doStuff() throws IllegalArgumentException { throw new IllegalArgumentException("Something wrong !!!"); // exception propagated to main() method } public static void main(String args[]) throws IllegalArgumentException { // // if not handled in main() method exception propagated to // defaultExceptionHandler System.out.println("main() begin"); Example03CallerDeclareUncheckedException obj = new Example03CallerDeclareUncheckedException(); obj.doStuff(); System.out.println("main() end"); } } |
Output:
main() begin
Exception in thread “main” java.lang.IllegalArgumentException: Something wrong !!!
at com.vidvaan.corejava.exception22.throwskeyword.Example03CallerDeclareUncheckedException.doStuff(Example03CallerDeclareUncheckedException.java:9)
at com.vidvaan.corejava.exception22.throwskeyword.Example03CallerDeclareUncheckedException.main(Example03CallerDeclareUncheckedException.java:18)
In the above example, doStuff() method declares unchecked IllegalArgumentException. Here caller main() method also declared IllegalArgumentException instead handling it. As no one handles IllegalArgumentException program got terminated.
Whether it is checked, unchecked or error if we don’t handle it some where in its call stack, it will terminate the program
Always handle exception
Example 04: throws keyword – method declare checked exception
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.vidvaan.corejava.exception22.throwskeyword; import java.io.IOException; // throws keyword - method declare checked exception public class Example04DeclareCheckedException { // declaring that there would be chance of IOException if some one // calls this method public void doStuff() throws IOException { throw new IOException("Something wrong !!!"); // exception propagated to main() method } public static void main(String args[]) { // // if not handled in main() method exception propagated to // defaultExceptionHandler System.out.println("main() begin"); Example04DeclareCheckedException obj = new Example04DeclareCheckedException(); obj.doStuff(); System.out.println("main() end"); } } |
Compilation Error :
Unhandled exception type IOException
In the above example, doStuff() method declares checked IOException. We are getting compilation error in main() method at the line we are calling doStuff() method. As doStuff() method is declared checked exception, the rule is main() method either handle it or declare it. Otherwise program won’t get compiled.
If you are calling a method that declares checked exception, you must either caught or declare the exception.
Case1: Caller caught the exception i.e. handle the exception using try/catch.
Case2: Caller declare the exception i.e. specifying throws with the method.
Example 05: throws keyword – caller handle checked exception
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package com.vidvaan.corejava.exception22.throwskeyword; import java.io.IOException; //throws keyword - caller handle checked exception public class Example05CallerHandleCheckedException { // declaring that there would be chance of IOException if some one // calls this method public void doStuff() throws IOException { throw new IOException("Something wrong !!!"); // exception propagated to main() method } public static void main(String args[]) { // // if not handled in main() method exception propagated to // defaultExceptionHandler System.out.println("main() begin"); Example05CallerHandleCheckedException obj = new Example05CallerHandleCheckedException(); try { obj.doStuff(); } catch (IOException e) { System.out.println(e.getMessage()); } System.out.println("main() end"); } } |
Output :
main() begin
Something wrong !!!
main() end
In the above example, doStuff() method declares checked IOException. We are handling IOException in main() method.
Example 06: throws keyword – caller declare checked exception
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.vidvaan.corejava.exception22.throwskeyword; import java.io.IOException; //throws keyword - caller declare checked exception public class Example06CallerDeclareCheckedException { // declaring that there would be chance of IOException if some one // calls this method public void doStuff() throws IOException { throw new IOException("Something wrong !!!"); // exception propagated to main() method } public static void main(String args[]) throws IOException { // // if not handled in main() method exception propagated to // defaultExceptionHandler System.out.println("main() begin"); Example06CallerDeclareCheckedException obj = new Example06CallerDeclareCheckedException(); obj.doStuff(); System.out.println("main() end"); } } |
Output :
main() begin
Exception in thread “main” java.io.IOException: Something wrong !!!
at com.vidvaan.corejava.exception22.throwskeyword.Example06CallerDeclareCheckedException.doStuff(Example06CallerDeclareCheckedException.java:11)
at com.vidvaan.corejava.exception22.throwskeyword.Example06CallerDeclareCheckedException.main(Example06CallerDeclareCheckedException.java:20)
In the above example, doStuff() method declares checked IOException. The caller method main() also declared IOException. As no one handles IOException, program got terminated. So we should always handle the exception some where in the application as per business need.
Example 07: throws keyword – declare error – catch error
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com.vidvaan.corejava.exception22.throwskeyword; //throws keyword - declare error - catch error public class Example07DeclareErrorCatchError { // declaring that there would be chance of AssertionError if some one // calls this method public void doStuff() throws AssertionError { throw new AssertionError("Something wrong !!!"); // error propagated to main() method } public static void main(String args[]) { // // if not handled in main() method error propagated to // defaultExceptionHandler System.out.println("main() begin"); Example07DeclareErrorCatchError obj = new Example07DeclareErrorCatchError(); try { obj.doStuff(); } catch (AssertionError e) { System.out.println(e.getMessage()); } System.out.println("main() end"); } } |
Output :
main() begin
Something wrong !!!
main() end
In the above example, doStuff() method declares AssertionError. We are handling AssertionError in main() method.
In the case of ‘throws’ keyword Error classes also behave same like a unchecked exceptions.
We rarely declare Error classes in our regular programming practices. But it is allowed as shown above.