The throws keyword is used to handle checked exceptions. As we learned in the previous article that exceptions are of two types: checked and unchecked. Checked exception (compile time) needs to be handled else the program won’t compile. On the other hand unchecked exception (Runtime) doesn’t get checked during compilation. Throws keyword is used for handling checked exceptions. You can declare multiple exceptions using throws keyword.
The throws keyword vs try-catch in Java
You may be wondering why we need throws keyword when we can handle exceptions using try-catch block in Java. Well, thats a valid question. We already know we can handle exceptions using try-catch block.
The throws keyword does the same thing that try-catch does but there are some cases where you would prefer throws over try-catch. For example: Lets say we have a method myMethod()
the statements inside this method can throw either ArithmeticException or NullPointerException, in this case you can use try-catch as shown below:
public void myMethod() { try { // Statements that might throw an exception } catch (ArithmeticException e) { // Exception handling statements } catch (NullPointerException e) { // Exception handling statements } }
But suppose you have several such methods that can cause exceptions, in that case it would be tedious to write these try-catch for each method. The code will become unnecessary long and will be less-readable.
One way to overcome this problem is by using throws like this: declare the exceptions in the method signature using throws and handle the exceptions where you are calling this method by using try-catch.
Another advantage of using this approach is that you will be forced to handle the exception when you call this method, all the exceptions that are declared using throws, must be handled where you are calling this method else you will get compilation error.
public void myMethod() throws ArithmeticException, NullPointerException { // Statements that might throw an exception } public static void main(String args[]) { try { myMethod(); } catch (ArithmeticException e) { // Exception handling statements } catch (NullPointerException e) { // Exception handling statements } }
Example of throws Keyword
In this example the method myMethod() is throwing two checked exceptions so we have declared these exceptions in the method signature using throws Keyword. If we do not declare these exceptions then the program will throw a compilation error.
import java.io.*; class ThrowExample { void myMethod(int num)throws IOException, ClassNotFoundException{ if(num==1) throw new IOException("IOException Occurred"); else throw new ClassNotFoundException("ClassNotFoundException"); } } public class Example1{ public static void main(String args[]){ try{ ThrowExample obj=new ThrowExample(); obj.myMethod(1); }catch(Exception ex){ System.out.println(ex); } } }
Output:
java.io.IOException: IOException Occurred
For more examples on throws refer this tutorial: throws examples.
gowthami says
i want to know the difference between checked and unchecked exception?
then how to rethrow an exception?
why we are rethrowing an exception?
explain with suitable exaple
yu song says
This is a real excellence website for Java beginners. it makes the concepts easy to understand. and also provide the simple examples to tell how to use them. i appreciate the writer/web owner’s effort. Thanks.
priyanka duggirala says
you are superb sir. I can easily understand the concepts I felt very difficult till now. I cannot express my happiness when I understand the concepts.
Thank you so much
rossi says
Output is Null pointer Exception in the full THROWS Example
Santhosh says
The best site for learning. Good stuff with easy explanation.
Murali Bojanki@cts says
Really Awesome It is Good to Understanding Everyone if we know bit knowledge in java concepts, really wonderful and helful to every one.