7 Basic Difference between throw and throws in Java | Example

7 Basic Difference between throw and throws in Java | Example

We have seen earlier how exception handling work in Java. Exception handling in Java assures that the flow of the program does not break when an error/exception occurs. As a result, Exception Handling makes Java programs robust.

Exceptions in Java can be handled with the help of keywords like

  • try
  • catch
  • throw
  • throws
  • finally

The two most confusing terms are throw and throws.

In this tutorial, let us get to know the major difference between the keywords ‘throw’ and ‘throws’ of exception handling with the help of an example.

Difference between throw and throws in Java

throws versus throw in tabular form

Basis of difference throw in Java throws in Java
1 Definition The ‘throw’ keyword is used throw an exception explicitly Contrarily, the ‘throws’ keyword is used to declare an exception, which is analogous to try-catch block.
2 Syntax The ‘throw’ keyword is followed by an instance of the Exception class.

Example:

throw new IOException();
The ‘throws’ keyword is followed by the name of the Exception class name.

Example:

void example () throws IOException { …. }

3 Point of practice The ‘throw’ keyword is used inside the method body. In simple words, we use it to throw an Exception logically.

void exampleMethod() {
try {
	throw new IOException();
} catch(Exception exception) {
	System.out.println(“I/O error:” +exception);
}
Contrarily, the ‘throws’ keyword is used along with the method declaration/signature. It is generally written in method signature when it is known that some statements inside the method may lead to exceptions.

void example () throws IOException {
//Method body
}

4 Count of exceptions thrown The ‘throw’ keyword can throw only one exception at a time.
For instance,

throw new IOException();
The ‘throws’ keyword can throw multiple exceptions at a time.

throws IOException, NullPointerException {
//method body.
}
5 Type of exceptions propagated The ‘throw’ keyword cannot propagate checked exceptions at all. It only propagates the unchecked exceptions.

void example() throws FileNotFoundException {
try {
		throw new IOException(); // as it is unchecked in throws.
	} catch(Exception exception) {
		System.out.println(“Exception Found: ”+exception);
	}
}

On the contrary, the ‘throws’ keyword can propagate only the checked exceptions.

void example() throws Arithmetic Exception { .... }

6. Example of throw | Java Code

throw
import java.lang.ArithmeticException;

public class ThrowExample {  
  void scanAge(int age) {  
    if(age<18)  
      throw new ArithmeticException("Cannot Vote. A voter needs to be 18 years and above.");  
    else  
      System.out.println("The person is 18 and above. He/She can cast vote.");  
   }

   public static void main(String args[]) {
	ThrowExample example = new ThrowExample();
	example.scanAge(15);  
 }
}

Output:

The expected output of the above code snippet would be:

Exception in thread "main" java.lang.ArithmeticException: Cannot Vote. A voter needs to be 18 years and above.
at ThrowExample.scanAge(ThrowExample.java:6)
at ThrowExample.main(ThrowExample.java:12)

7. Example of throws | Java Code

import java.lang.ArithmeticException;

public class ThrowsExample {
  int divisionByZero(int value1,int value2) throws ArithmeticException {
    int result = value1/value2;
    return result;
  }

  public static void main(String[] args) {
    ThrowsExample example = new ThrowsExample();
    try {
      example. divisionByZero(4,0);
    } 
    catch(ArithmeticException exception) {
      System.out.println("A number cannot be divided by 0!");
  }
}

Output:

The expected output of the above code snippet would be:

A number cannot be divided by 0!

And that’s a wrap to the major difference between throw and throws in Java as a part of Exception Handling in Java. Both the keywords play a significant role in handling exception in Java.

Do you find this article useful for your learning? Let me know your thought in the comment.

Happy Learning!

Leave a Reply

Your email address will not be published. Required fields are marked *