In this guide, we will see few examples of throws keyword. I highly recommend you to read my detailed guide on throws keyword before going through these examples so that you have a better understand of the this concept.
Read these guides to learn exception handling from scratch:
Example 1: Exception propagation using throws keyword
In this example we are seeing an example of throws keyword in exception propagation. Here, an exception occurred in method1()
which has been handled in the chain-calling method method3()
.
This example shows how exception propagation works. If the exception is not handled in the method then compiler checks whether the exception is handled in the calling method.
Here compiler didn’t find exception handling in method1()
, so it checked in the calling method method2()
, then it checked in the calling method method3()
, where the exception is handled. The exception propagation concept is explained in detail here.
class Example1{ void method1() throws ArithmeticException{ throw new ArithmeticException("Calculation error"); } void method2() throws ArithmeticException{ method1(); } void method3(){ try{ method2(); } catch(ArithmeticException e){ System.out.println("ArithmeticException handled"); } } public static void main(String args[]){ Example1 obj=new Example1(); obj.method3(); System.out.println("End Of Program"); } }
Output:
ArithmeticException handled End Of Program
Example 2: throw and throws keyword example
In this example, we are using both throw keyword and throws keyword. We have declared the IOException using throws keyword and using the throw keyword to raise the exception in the myMethod().
import java.io.*; class Demo{ void myMethod()throws IOException{ //throw exception using throw keyword throw new IOException("IO Exception occurred"); } } class JavaExample{ //declared IOException using throws keyword public static void main(String args[])throws IOException{ Demo obj = new Demo(); obj.myMethod(); System.out.println("Rest of the program"); } }
Output:
Example 3: When you don’t handle exception that is declared using throws
The ideal way to use throws is by declaring the exceptions in method signature and handle the exceptions using try-catch in calling method. Let’s see what happens when we declare the exception at both the places, in method signature as well as in calling method.
class ExceptionExample{ void method()throws ArithmeticException{ throw new ArithmeticException("ArithmeticException Occurred"); } } class Example1{ public static void main(String args[])throws ArithmeticException{ ExceptionExample obj=new ExceptionExample(); obj.method(); System.out.println("End Of Program"); } }
Output:
Exception in thread "main" java.lang.ArithmeticException: ArithmeticException Occurred at ExceptionExample.method(Example1.java:4) at Example1.main(Example1.java:10)
anand says
can we declare unchecked exception using throws keyword?
Ajeet kumar says
no you can’t
Cheta says
I am trying to write the same code within same class. how come it’s going to catch block every time?