How do we Handle Exception in Java

By | 2018-04-13T22:09:26+00:00 April 24th, 2015|java|0 Comments

Exception Handling in Java

In the previous tutorial, we have discussed about what is an Exception and different types of exceptions in Java. In this tutorial we are going to see how to handle the Exception in Java.

Typical Exception Handling Work Flow :

Exception-Flow

To handle exceptions, Java gave us following keywords.

  1. try
  2. catch
  3. finally
  4. throw
  5. throws

try block

In Java when we are not sure about the outcome of a statement, we can try out that statements (not sure about the out come) by placing in a try block. A try block can be used in a method or an another try block.

Syntax:

public void someMethod(){
          try{
                   // Statements
          }catch(Exception e){
                // Handling an Exception

          }
}

catch block

In Java catch block is used to handle the exception. While executing the statements in try block, if the system encounters an exception then it skips out rest of the statements and transfer the control to corresponding catch block. Hence the catch block is executed, if and only if an exception is raised other wise it will not be executed.

Once the control comes out from the try block, there is no way of taking it back in to try block.

finally block

A finally block is a block of statements, that are executed irrespective of whether we get an exception or not. Hence the statements in the finally block are executed compulsory. Typically finally blocks are used to nullify the object references and closing the io streams.

Syntax :

public void someMethod(){
          try{
                   // Statements
          }catch(Exception e){
                // Handling an Exception

          }finally{
               // Statements
          }
}

throw

The throw keyword is used to raise the exception explicitly and explicit exception propagation. Only Throwable or sub classes of Throwable can be thrown.

Syntax:

public void someMethod(){
          try{
                   // Statements
          }catch(Exception e){
                // Handling an Exception
                throw new ArithmeticException("I am Throwing.");

          }finally{
               // Statements
          }
}

throws

If a method is capable of generating Checked Exception that if we doesn’t know how to handle it then we can throws that checked exception by using the throws keyword.

Exception Handling Example


public class ThrowsAndThrowDemo {

    public static void main(String[] args) throws InterruptedException {
        try {
            System.out.println("First Statement");
            if (true) {
                Thread.sleep(1000);
                throw new java.io.IOException();
            }
            System.out.println("Another Statement");

        } catch (java.io.IOException e) {
            e.printStackTrace();
        } finally {
            System.out.println("In Finally Block");
        }
    }
}

Output:

First Statement 
java.io.IOException at ThrowsAndThrowDemo.main(ThrowsAndThrowDemo.java:13) 
In Finally Block

Happy Learning 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment