Monday, May 23, 2022

throws Keyword in Java Exception Handling

If you want to leave it to the caller method to handle any exception thrown by the called method it can be done using throws keyword in Java.

A method can just declare the exception it may throw using the throws keyword and callers of the method have to provide exception handling for those exceptions (or they can also declare them using throws).

Method declaration syntax that includes a throws clause

type method-name(parameter-list) throws exception-list
{
// body of method
}

Here, exception-list is a comma-separated list of the exceptions that a method can throw.

Java throws clause example

Let's say you want to read a file in Java using BufferedReader which forces you to handle IOException. Now if you want to leave it to the caller method to handle that exception you can declare it using throws keyword.

public class ThrowsDemo {
 public static void main(String[] args) {  
   try {
    displayFile();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }  
 }
 
 static void displayFile() throws IOException{ 
  String strLine;
  BufferedReader br = new BufferedReader(new java.io.FileReader("F:\\abc.txt"));
  while((strLine = br.readLine()) != null){
   System.out.println("Line is - " + strLine);
  }
 }
}

In the above code you can see that the displayFile() method declares the IOException using throws clause. Then in main method (which is the caller method) you need to enclose the method call in try-catch block to handle the error.

Throws keyword with checked and unchecked exceptions

Throws clause in Java is necessary for all checked exceptions only. If a method throws checked exceptions and does not provide any exception handling for it using try-catch block with in the method, then those exceptions must be declared using throws clause. If they are not, it will result in a compile-time error.

For unchecked exception anyway compiler doesn't force any try-catch block so throws is also optional, not declaring unchecked exception in a throws clause won't result in any compiler error.

When Throws clause not used with Checked exception

In the above code if throws is removed from displayFile() method.

throws keyword in Java

It can be seen that it results in compiler error as the method has no try catch block to handle exception and it is not even declaring the thrown exceptions using throws clause.

How throws keyword help

If you are writing some framework classes or a library that is used by other developers, you won't be able to anticipate how other users want to handle exceptions. In this case, it's better to not catch the exception in the method where it is thrown and leave it to a caller method further up the call stack to handle it.

Points to remember

  • The throws keyword is followed by a comma-separated list of all the exceptions thrown by that method.
  • Including unchecked exceptions in the throws clause is not mandatory.

That's all for this topic throws Keyword in Java Exception Handling. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Difference Between throw And throws in Java
  2. Multiple Catch Blocks in Java Exception Handling
  3. Nested Try Statements in Java Exception Handling
  4. Creating Custom Exception Class in Java
  5. Java Exception Handling Interview Questions And Answers

You may also like-

  1. Polymorphism in Java
  2. Marker Interface in Java
  3. Covariant Return Type in Java
  4. How HashMap Works Internally in Java
  5. Fail-Fast Vs Fail-Safe Iterator in Java
  6. Lambda Expression Examples in Java
  7. Interface Static Methods in Java
  8. Why wait(), notify() And notifyAll() Must be Called Inside a Synchronized Method or Block

No comments:

Post a Comment