Difference between throw and throws in Java
Prerequisite: Throw and Throws in Java
The differences between throw and throws are:
- Point of usage
- throw keyword is used inside a function. It is used when it is required to throw an Exception logically.
voidDemo()throwsArithmeticException, NullPointerException{// Statements where exceptions might occur.thrownewArithmeticException();}chevron_rightfilter_none - throws keyword is in the function signature. It is used when the function has some statements that can lead to some exceptions.
voidDemo(){// Statements where exceptions might occur.}chevron_rightfilter_none
- throw keyword is used inside a function. It is used when it is required to throw an Exception logically.
- Number of exceptions thrown
- throw keyword is used to throw an exception explicitly. It can throw only one exception at a time.
For example:
// throwing only an IOExceptionthrownewIOException();chevron_rightfilter_none - throws keyword can be used to declare multiple exceptions, separated by comma. Whichever exception occurs, if matched with the declared ones, is thrown automatically then.
For example:// throwing multiple exceptionsvoidDemo()throwsArithmeticException, NullPointerException{// Statements where exceptions might occur.}chevron_rightfilter_none
- throw keyword is used to throw an exception explicitly. It can throw only one exception at a time.
- Syntax
- Syntax of throw keyword includes the instance of the Exception to be thrown.
For example:// throwing instance of IOExceptionthrownewIOException();chevron_rightfilter_none - Syntax of throws keyword includes the class names of the Exceptions to be thrown.
For example:// throwing multiple exceptions by class namesvoidDemo()throwsArithmeticException, NullPointerException{// Statements where exceptions might occur.}chevron_rightfilter_none
- Syntax of throw keyword includes the instance of the Exception to be thrown.
- Propagation of Exceptions
- throw keyword cannot propagate checked exceptions. It is only used to propagate the unchecked Exceptions that are not checked using throws keyword.
For example:voidmain()throwsIOException{// Using throw for ArithmeticException// as it is unchecked in throwsthrownewArithmeticException();}chevron_rightfilter_none - throws keyword is used to propagate the checked Exceptions only.
For example:voidmain()throwsIOException{}chevron_rightfilter_none
- throw keyword cannot propagate checked exceptions. It is only used to propagate the unchecked Exceptions that are not checked using throws keyword.
- Example:
- throw:
publicclassGFG {publicstaticvoidmain(String[] args){// Use of unchecked Exceptiontry{// double x=3/0;thrownewArithmeticException();}catch(ArithmeticException e){e.printStackTrace();}}}chevron_rightfilter_noneOutput:
java.lang.ArithmeticException: / by zero at UseOfThrow.main(UseOfThrow.java:8) - throws:
importjava.io.IOException;publicclassUseOfThrowAndThrows {publicstaticvoidmain(String[] args)throwsIOException{}}chevron_rightfilter_noneOutput:
Exception in thread "main" java.io.IOException at UseOfThrowAndThrows.main(UseOfThrow.java:7)throw throws throw keyword is used to throw an exception explicitly. throws keyword is used to declare one or more exceptions, separated by commas. Only single exception is thrown by using throw. Multiple exceptions can be thrown by using throws. throw keyword is used within the method. throws keyword is used with the method signature. Syntax wise throw keyword is followed by the instance variable. Syntax wise throws keyword is followed by exception class names. Checked exception cannot be propagated using throw only.Unchecked exception can be propagated using throw. For the propagation checked exception must use throws keyword followed by specific exception class name.
Recommended Posts:
- throw and throws in Java
- Try, catch, throw and throws in Java
- Output of Java Programs | Set 44 (throws keyword)
- Using throw, catch and instanceof to handle Exceptions in Java
- Difference between x++ and x=x+1 in Java
- Difference between Java and JavaScript
- Difference between while and do-while loop in C, C++, Java
- Difference between Java and C language
- Difference between super() and this() in java
- Difference between Groovy and Java
- Difference between == and .equals() method in Java
- Difference between ArrayList and HashSet in Java
- Difference Between LinkedList and LinkedHashSet in Java
- Difference between notify() and notifyAll() in Java
- Difference between concat() and + operator in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
- throw:


