In this article, we will discuss difference between throw and throws clause in detail with few examples
Already discussed throw clause & throws clause in earlier articles, let us re-visit key points before going for difference between them
throw clause or throw keyword:
- throw keyword is used to throw exception explicitly
- It is used within method to throw exception explicitly
- It is generally used for throwing user-defined exception or custom exception
- Although, it is valid & possible to throw pre-defined exception or already defined exception in Java too
- Maximum of only one exception can be thrown using throw keyword and it can be checked exception or unchecked exception or used-defined exception
- throw keyword is always followed by instance (i.e.; instance of any type of exception)
- Syntax:
throw instanceOfExceptionType;
throws clause or throw keyword:
- throws keyword is used to declare exception, indicating caller-method to handle exception whenever invoking
- with the usage of throws clause, any type of exception can be declared (i.e.; checked exception or unchecked exception or user-defined exception)
- Any number of exception can be declared next to method signature, with comma (,) separating them
- throws keyword is always followed by class (this class must be pre-defined exception or user-defined exception which must be sub-class of Throwable class or one of its sub-class)
- Syntax:
access-modifier return-type method-name() throws exception-list;
Let us move on and discuss them on one-on-one parameter in the tabular format
throw v/s throws:
throw clause/keyword | throws clause/keyword |
throw keyword is used to throw exception explicitly | throws keyword is used to declare exception to delegate/indicate exception handling responsibility to caller-method |
throw keyword is always followed by instance of Throwable type or exception type | throws keyword is always followed by exception list (with comma separating them) |
throw keyword is used within method i.e.; to throw exception from try-catch block enclosed within method | throws keyword is used next to method signature |
Syntax:
throw instanceOfExceptionType; |
Syntax:
access-modifier return-type method-name() throws exception-list; |
Maximum of only one exception can be thrown using throw keyword
Thrown exception can be checked exception or unchecked exception or user-defined exception |
Any number of exception can be declared (to be thrown) using throws keyword But they are all separated by comma (,) |
Example on throw & throws keyword:
- Whenever checked-exception (it may be pre-defined or user-defined exception) is thrown explicitly using throw keyword, then it must be handled either using try-catch block or throws clause. Therefore, we have used throws clause to delegate the exception responsibility to caller-method
- But whenever unchecked-exception (it may be pre-defined or user-defined exception) is thrown explicitly using throw keyword, then it is not necessary to handle. It is up to the choice of programmer to handle it
Example 1: checked exception
ThrowAndThrowsExample.java
package in.bench.resources.exception.handling; import java.io.FileNotFoundException; public class ThrowAndThrowsExample { public static void main(String[] args) throws FileNotFoundException { // must be surrounded with try-catch block compulsorily, // because we are invoking method throwing // checked-exception OR throws clause printFileContent(); } // throwing checked exception public static void printFileContent() throws FileNotFoundException { // assumed that, // we are trying to access file from remote location // FileReader fileReader = // new FileReader("D:/Folder/test.txt"); throw new FileNotFoundException("File is not available"); // further file processing } }
Example 2: unchecked exception
- Explicitly throwing exception using throw keyword
ThrowWithUncheckedExceptionExample.java
package in.bench.resources.exception.handling; public class ThrowWithUncheckedExceptionExample { public static void main(String[] args) { // invoking method anotherMethod(null); } public static void anotherMethod(String str) { if(str == null){ throw new NullPointerException("Please send some valid String"); } // further processing with the string value } }
References:
- https://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html
- https://docs.oracle.com/javase/tutorial/essential/exceptions/declaring.html
- https://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html
- https://docs.oracle.com/javase/tutorial/essential/exceptions/try.html
- https://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html
- https://docs.oracle.com/javase/tutorial/essential/exceptions/
- https://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/Error.html
- https://docs.oracle.com/javase/specs/jls/se7/html/jls-11.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/ArithmeticException.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/NullPointerException.html
- http://docs.oracle.com/javase/7/docs/api/java/lang/ArrayIndexOutOfBoundsException.html
- http://www.oracle.com/technetwork/java/effective-exceptions-092345.html
- http://otfried.org/courses/cs206/slides/slides-stackframes.pdf
Read Also:
- Exception Handling in Java
- Exception Hierarchy in Java
- Checked Exception v/s Unchecked Exception
- return statement with finally block
- final v/s finally v/s finalize
- throw keyword in Java
- throws keyword in Java
Happy Coding !!
Happy Learning !!