Tuesday , February 28 2017
Home / java / Exception handling in Java

Exception handling in Java

Exception handling in Java

In the previous tutorial we have discussed about what is Exception in Java ? that gives a basic knowledge about the Exception, Error and all the stuff. In this tutorial we need to discuss some more about how to handle the Exception in Java. In Java we have the following keywords to handling the Exceptions.

  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 the statement by placing in a try block. A try block can be used in a method or an another try block. A try block may have any number of catch blocks. But there is no existence of try block with out catch or finally blocks.

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 it doesn’t want to handle it, mentioned the same to the JVM with the throws keyword.

A typical Exception Flow Diagram

 

Exception-Flow

Example


/**
 *
 * @author chandrashekhar
 */
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");
        }
    }
}

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

 

About chandrashekhar

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+ ,

Check Also

Type Inference in Java 7 Example

Type Inference in Java 7 is another great addition introduced to ease the developer to type …

Leave a Reply

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