January 9, 2024

Java throws Clause With Examples

Code in your method may throw exceptions that your method doesn’t handle. In that case you need to specify those exceptions along with the method declaration. That way calling method can provide exception handling mechanism for those exceptions. To specify the exceptions in your method declaration you can use throws keyword in Java.

General form of throws keyword in Java

type methodName(parameters) throws exception1, excpetion2...{
  ...
  ...
}

try-catch block or throws

You can handle the exception thrown in your method code with in the method by providing a try-catch block. If you want to delegate it to the caller method to provide exception handling mechanism, you can declare the exceptions using throws in Java. Then it is the responsibility of the calling method to provide exception handling mechanism. Of course caller method can also declare the exception using throws keyword and delegate it to the next method in the stack to handle it.

As per the best practices for exception handling you should always throw early and catch late. So it is advisable to use throws clause in order to catch late.

Checked exceptions and throws clause

It is mandatory to specify all the checked exceptions using throws clause in your method declaration if exceptions are not handled with in the method. Not doing so will result in compile time error.

For unchecked exceptions specifying them in throws clause is optional. There won’t be any compile time error if you don’t specify unchecked exceptions with throws clause.

Example code when throws is not used with Checked exception

throws in Java

In the code there is neither try-catch block to handle the exception nor the throws clause to specify the exception, thus the compile time error because FileNotFoundException is a checked exception.

throws clause Java Example

The above code can be written as follows to use throws clause.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class ThrowsDemo {
  public static void main(String[] args) {
    ThrowsDemo td = new ThrowsDemo();
    try {
      td.readFile();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
	
  private void readFile() throws IOException{
    BufferedReader br = null;
    try{
      br = new BufferedReader(new InputStreamReader(new FileInputStream(
                               new File("D:\\test1.txt"))));
    }finally{
      br.close();
    }		
  }
}

In the above code you can see that the try-finally is still used as resource is closed in the finally block. Here readFile() method can throw two checked exceptions FileNotFoundException when trying to open the file and IOException when trying to close the BufferedReader in the finally block. Rather than providing try-catch blocks to handle those exceptions throws clause is used to declare the thrown exception. Note that IOException is the parent class of FileNotFoundException so you can declare only IOException to take care of both of these exceptions.

Important points

  1. Using throws clause you can specify the exception thrown by the method code with in the method declaration.
  2. throws in Java exception handling delegates the responsibility of exception handling to the caller method.
  3. For checked exceptions it is mandatory to either provide try-catch block to handle the exception or declare it using throws. Not doing so will result in compile time error.
  4. For unchecked exceptions it is not mandatory to declare them with throws clause.

That's all for the topic Java throws Clause With Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment