Tutorials

Java Exception Handling


throws


Your code might throw one or more exception(s).

In case you do not wish to handle exception yourself thrown by the method of your class then its always a good idea to declare the possible exceptions that your code might throw. 

 

You can declare the exceptions that your code might throw using throws keyword at method level

This will help the caller of your method be aware of the exceptions that may occur and will help them to handle thise exception.

 

Syntax of throws


AccessModifier  returnType  methodName() throws ExceptionClass1, ExceptionClass2, ...., ExceptionClassN{

   //code that might throw exception

}

Sample example


public void pickFile() throws FileNotFoundException {

    //code that might throw an Exception

}

 

Important points on throws

  • If you do not handle exception or declare checked Exception using throws keyword at the method level then you will get compile time error.
  • Using throws keyword you can declare multiple Exceptions at a time. See the syntax for reference.
  • Throws keyword are used to declare checked exception.

Advantage of throws

  • In case you want the caller of the method to handle the exception that may be thrown by your method, declare the possible exceptions (s) that your code might throw using throws keyword.  If the caller does not handle the exception properly then they will get compile time error in their code.

So caller of the method has two options available:

  1. Handle exception using try-catch block,
  2. Declare the same exception using throws keyword on the method of your class. In this case run time exception will be thrown as throws does not handle exception itself.

We will see both the scenarios with an example.

 

Example 1: Handle exception using try-catch block while calling method having throws declaration


package exceptionHandling;

import java.io.FileNotFoundException;

public class ThrowsExample {

      public static void main(String[] args) {

            try{
                  Rajneesh raj = new Rajneesh();
                  raj.methodR();
            }catch(Exception e){

                  System.out.println("Exception handled by try-catch");

            }

            System.out.println("Next line after try-catch");

      }

}



class Rajneesh{

      public void methodR() throws FileNotFoundException{

            throw new FileNotFoundException("File not found");

      }

}

OUTPUT:

Exception handled by try-catch
Next line after try-catch
 

 

Example 2: Declare the exception using throws keyword


package exceptionHandling;

import java.io.FileNotFoundException;

public class ThrowsExample {

      public static void main(String[] args) throws FileNotFoundException {
           
                  Rajneesh raj = new Rajneesh();
                  raj.methodR();

            System.out.println("Next line after try-catch");

      }

}



class Rajneesh{

      public void methodR() throws FileNotFoundException{

            throw new FileNotFoundException("File not found");

      }

}

OUTPUT:

Exception in thread "main" java.io.FileNotFoundException: File not found
at exceptionHandling.Rajneesh.methodR(ThrowsExample.java:24)
at exceptionHandling.ThrowsExample.main(ThrowsExample.java:10)
 
If you also declare exception which is declared by called method using throws keyword then two things is possible:
  1. Exception doesn't occur: In this case code will execute normally
  2. Exception occurs: In this case, run time exception will be thrown as in our code given above. It's because we have only declared exception at main method and not handled it. Had we handled it using try-catch block things would have gone normal.

Remember, throws just declares exception that your method might throw and leaves the responsibility of handling it on caller class.

throws keyword doesn't handle exception itself.

 

 



Please Share this page
Views : 45