Difference between throw and throws in Java

In this guide, we will discuss the difference between throw and throws keywords. Before going though the difference, refer my previous tutorials about throw and throws.

Throw vs Throws in java
  1. Throws clause is used to declare an exception, which means it works similar to the try-catch block. On the other hand throw keyword is used to throw an exception explicitly.

  2. If we see syntax wise than throw is followed by an instance of Exception class and throws is followed by exception class names.
    For example:

    throw new ArithmeticException("Arithmetic Exception"); and throws ArithmeticException;

  3. Throw keyword is used in the method body to throw an exception, while throws is used in method signature to declare the exceptions that can occur in the statements present in the method.
    For example:

    Throw: ... void myMethod() { try { //throwing arithmetic exception using throw throw new ArithmeticException("Something went wrong!!"); } catch (Exception exp) { System.out.println("Error: "+exp.getMessage()); } } ... Throws: ... //Declaring arithmetic exception using throws void sample() throws ArithmeticException{ //Statements } ...

  4. You can throw one exception at a time but you can handle multiple exceptions by declaring them using throws keyword.
    For example:

    void myMethod() { //Throwing single exception using throw throw new ArithmeticException("An integer should not be divided by zero!!"); } .. Throws: //Declaring multiple exceptions using throws void myMethod() throws ArithmeticException, NullPointerException{ //Statements where exception might occur }
    These were the main differences between throw and throws in Java. Lets see complete examples of throw and throws keywords.

Throw Example
public class Test { void checkAge(int age) { if(age<18) throw new ArithmeticException("Not Eligible for voting"); else System.out.println("Eligible for voting"); } public static void main(String args[]){ Test obj = new Test(); obj.checkAge(15); System.out.println("End Of Program"); } }


Output:
Exception in thread "main" java.lang.ArithmeticException: Not Eligible for votin g at Test.checkAge(Test.java:6) at Test.main(Test.java:12)
Throws Example
public class Test { int division(int a, int b) throws ArithmeticException { int t = a/b; return t; } public static void main(String args[]){ Test obj = new Test(); try{ System.out.println(obj.division(15,0)); } catch(ArithmeticException e){ System.out.println("You shouldn't divide number by zero"); } } }


Output:
You shouldn't divide number by zero
No.throwthrows
1)Java throw keyword is used to explicitly throw an exception.Java throws keyword is used to declare an exception.
2)Checked exception cannot be propagated using throw only.Checked exception can be propagated with throws.
3)Throw is followed by an instance.Throws is followed by class.
4)Throw is used within the method.Throws is used with the method signature.
5)You cannot throw multiple exceptions.You can declare multiple exceptions e.g.
public void method()throws IOException,SQLException.



Instagram