«Back to Home

Core Java

Topics

Throws Keyword In Java

Throws Keyword
 
Throws keyword is used for handling checked exception with the using of throws we can declare multiple exceptions simultaneously and if any unchecked exception occurs like NullPointerException. Thus, it is programmers responsibility that he is not check the code properly before the code being used.
 
In Java, throws is mainly used to declare an exception, which gives an information to the developer that there may occur an exception. Thus, it is better for the programmer to provide the exception handling code. Thus, the normal flow of the program can be maintained.
 
Syntax

return_type method_name() throws exception_class_name{
//method code
}
 
Which types of exceptions should be declared by throws keyword

Only checked exception should be declared by throws keyword because,
  • Unchecked Exception: If the programmer commits any programming mistake then only he can rectify unchecked exception.

  • Error: Errors are beyond our control. For example, VirtualMachineError or StackOverflowError occurs.
Advantage of Java throws keyword

With the usage of throws checked exception, it can be propagated and provides the information to the caller of the method about the exception.
 
Let's see an example, given below of checked exceptions, which can be propagated by throws keyword.
 
Code
  1. package exceptionHandling;  
  2. import java.io.IOException;  
  3. public class ThrowsExample {  
  4.     void msg() throws IOException {  
  5.         throw new IOException("device error"); //checked exception  
  6.     }  
  7.     void call() throws IOException {  
  8.         msg();  
  9.     }  
  10.     void phone() {  
  11.         try {  
  12.             call();  
  13.         } catch (Exception e) {  
  14.             System.out.println("exception handling....");  
  15.         }  
  16.     }  
  17.     public static void main(String args[]) {  
  18.         ThrowsExample ts = new ThrowsExample();  
  19.         ts.phone();  
  20.         System.out.println("normal flow of the program...");  
  21.     }  
  22. }  
26

Output

27

In the example, shown above, we are calling a method that declares an exception. We must either caught or declare the exception in the program.
 
There are two important cases, which are,
 
Case1: If we handle the exception
 
Let’s see an example, given below.
 
Code
  1. package exceptionHandling;  
  2. import java.io.*;  
  3. class Throws1 {  
  4.     void msgDisplay() throws IOException {  
  5.         throw new IOException("device error");  
  6.     }  
  7. }  
  8. public class ThrowsExample {  
  9.     public static void main(String args[]) {  
  10.         try {  
  11.             Throws1 t = new Throws1();  
  12.             t.msgDisplay();  
  13.         } catch (Exception e) {  
  14.             System.out.println("exception handling.....");  
  15.         }  
  16.         System.out.println("Normal flow of the program...");  
  17.     }  
  18. }  
28
 
Output

29
 
In the example, mentioned above, we handle the exception due to which the code will be executed normally whether an exception occurs in the program.
 
Case2: If we declare the exception
  1. If we declare the exception and exception does not occur, the code executes normally.

  2. If we declare the exception and exception occurs, the exception will be thrown at the runtime because throws keyword does not handle the exception.
Let’s see an example, if exception does not occur, given below.
 
Code
  1. package exceptionHandling;  
  2.   
  3. import java.io.*;  
  4.   
  5. class Throws1 {  
  6.     void msgDisplay() throws IOException {  
  7.         System.out.println("device error");  
  8.     }  
  9. }  
  10. public class ThrowsExample {  
  11.   
  12.     public static void main(String args[]) throws IOException {  
  13.   
  14.         Throws1 t = new Throws1();  
  15.         t.msgDisplay();  
  16.         System.out.println("Normal flow of the program...");  
  17.     }  
  18. }  
30

Output

31
 
Let’s see an example, if an exception occurs, given below.
 
Code
  1. package exceptionHandling;  
  2.   
  3. import java.io.*;  
  4.   
  5. class Throws1 {  
  6.   
  7.     void msgDisplay() throws IOException {  
  8.         throw new IOException("device error");  
  9.     }  
  10. }  
  11.   
  12. public class ThrowsExample {  
  13.   
  14.     public static void main(String args[]) throws IOException {  
  15.   
  16.         Throws1 t = new Throws1();  
  17.         t.msgDisplay();  
  18.         System.out.println("Normal flow of the program...");  
  19.     }  
  20. }  
32
 
Output

33

One important question in an exception handling is can we rethrow an exception?
 
Yes, we can rethrow an exception by throwing same exception in catch block.
 
Summary

Thus, we learnt that throws is mainly used to declare an exception and also learnt their usage with the examples.