throws Clause in Java Exception Handling

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

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.

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 exception 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

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

Example Java code with throws clause

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

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 throws Clause in Java Exception Handling. If something is missing or you have something to share about the topic please write a comment.


You may also like

2 Comments

  1. Pingback: Difference Between throw And throws in Java - KnpCode

  2. Pingback: Exception Handling With Method Overriding in Java - KnpCode

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.