Read Also : Difference between Checked and Unchecked Exception in Java
Difference between throw and throws in Java
1. Definition: throw is a statement and used in a method to explicitly throw an exception.throws keyword is used by a method to specify which exceptions can be thrown from the method.
2. Place of declaration: First we need to understand what is method signature. Method signature is a part of method declaration. Method signature is a combination of method name and parameters list.
throw is used with in method. throws is used with the method signature.
/* throws keyword is used with the
method signature(combination of
method name and parameters list)
as shown below */
public void JavaHungry(String str)throws ArithmeticException, NullPointerException {
/* throw keyword is used inside method
named JavaHungry */
throw new ArithmeticException(); }
3. Syntax: throw is followed by an object(instance) of Exception class while throws is followed by Exception class names.
For example,
/* throws keyword is followed by ArithmeticException and NullPointerException, both of them are Exception class names*/ public void JavaHungry(String str)throws ArithmeticException, NullPointerException { /* throw keyword is followed by object(instance) of ArithmeticException as there is new keyword before the ArithmeticException*/ throw new ArithmeticException(); }
4. Number of exceptions thrown: throw statement can only throw one exception at a time.
throws can be used to throw multiple exceptions, separated by comma ( , ).
For example,
/* throws keyword is followed by multiple exceptions i.e. ArithmeticException and NullPointerException, both of them are separated by comma*/ public void JavaHungry(String str)throws ArithmeticException, NullPointerException { /* throw keyword is followed by only one exception i.e ArithmeticException */ throw new ArithmeticException(); }
5. Examples:
Throw statement example given below:
public class ThrowExample {
public static void checkPassEligibility(float totalMarks){ if(totalMarks > 33){ System.out.println("Student passed"); } else { throw new ArithmeticException("Student failed"); } }
public static void main(String[] args){ ThrowExample te = new ThrowExample(); System.out.print("Hi! Please check your score"); te.checkPassEligibility(31); System.out.println("Thank you!"); } }
Output :
Hi! Please check your score
Exception in thread "main" java.lang.ArithmeticException: Student failed
at ThrowExample.checkPassEligibility(ThrowExample.java:7)
at ThrowExample.main(ThrowExample.java:13)
Throws keyword example given below :
public class ThrowsExample { public int division(int numerator,int denominator) throws ArithmeticException{ int result = numerator/denominator; return result; } public static void main(String[] args){ ThrowsExample te=new ThrowsExample(); System.out.print("Division of numerator and denominator : "); te.division(50,0); } }
Output :
Division of numerator and denominator :
Exception in thread "main" java.lang.ArithmeticException: / by zero
at ThrowsExample.division(ThrowsExample.java:4)
at ThrowsExample.main(ThrowsExample.java:11)
Similarities between throw and throws in Java
1. Both throw and throws need to be handled and cannot be left uncaught.Recap : Difference between throw and throws in Java
Throw | Throws | |
---|---|---|
Definition | throw is a statement and it is used to explicitly throw an exception. | throws keyword is used to specify which exceptions can be thrown from the method. |
Place of declaration | throw is used inside method. | It is used with method signature. |
Syntax | It is followed by an object of the Exception class | It is followed by Exception class names. |
Number of Exceptions thrown | It can only throw one exception at a time. | It can throw multiple exceptions separated by a comma. |
That's all for the day, please mention in comments if you have any questions or doubts related to the article, the difference between throw and throws in java with examples.