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
1. 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;
2. throws-clause or throws 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
3. 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 (,) |
4. 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
4.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
}
}
4.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
}
}
Related Articles:
- Java – Exception Handling
- Java – Exception Hierarchy
- Java – 5 important keywords in Java Exception handling
- Java – Runtime mechanism, what happens when exception is thrown ?
- Java – Checked Exception v/s Unchecked Exception
- Java – Exception propagation
- Java – try-catch block
- Java – finally block
- Java – try with multiple catch blocks
- Java – Nested try-catch block
- Java – Returning value from method having try-catch-finally blocks
- Java – return statement with finally block
- Java – final v/s finally v/s finalize
- Java – Various methods to print exception information
- Java – throw keyword
- Java – throws keyword
- Java – throw v/s throws
- Java – Difference between throws clause and try-catch-finally block
- Java – Rules for Exception handling w.r.t Method Overriding
- Java – User-defined or Custom exception
- Java – Difference between ClassNotFoundException v/s NoClassDefFoundError
- Java – Top Exception and Error
- Java – Interview question and answers on Exception Handling
- Java 7 – try with resources
- Java 7 – multi-catch block
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
Happy Coding !!
Happy Learning !!