BeginnersBook

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
    • Learn jQuery
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Throws clause in java – Exception handling

By Chaitanya Singh | Filed Under: Exception Handling

As we know that there are two types of exception checked and unchecked. Checked exception (compile time) force you to handle them, if you don’t handle them then the program will not compile.
On the other hand unchecked exception (Runtime) doesn’t get checked during compilation. Throws keyword is used for handling checked exceptions . By using throws we can declare multiple exceptions in one go.

What is the need of having throws keyword when you can handle exception using try-catch?

Well, thats a valid question. We already know we can handle exceptions using try-catch block.
The throws 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() that has statements that 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.

❮ PreviousNext ❯

Comments

  1. gowthami says

    June 30, 2014 at 5:37 AM

    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

    Reply
  2. yu song says

    November 11, 2015 at 2:52 AM

    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.

    Reply
  3. priyanka duggirala says

    April 1, 2016 at 3:32 AM

    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

    Reply
  4. rossi says

    April 11, 2016 at 10:56 PM

    Output is Null pointer Exception in the full THROWS Example

    Reply
  5. Santhosh says

    April 23, 2016 at 4:06 AM

    The best site for learning. Good stuff with easy explanation.

    Reply
  6. Murali [email protected] says

    April 28, 2016 at 8:47 AM

    Really Awesome It is Good to Understanding Everyone if we know bit knowledge in java concepts, really wonderful and helful to every one.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Exception Handling

  • Exception handling
  • try-catch block
  • Multiple catch blocks
  • nested try-catch
  • finally block
  • Flow Control in try-catch-finally
  • throw keyword
  • throws clause
  • throw vs throws
  • Custom Exception
  • Checked and Unchecked Exceptions
  • Exception Examples

MORE ...

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap