Monday 24 February 2014

Java7 Support for Suppressed Exceptions

Throwable class provides one constructor and 2 methods to handle suppressed exceptions.

Constructor
protected Throwable( String message,
                                             Throwable cause,
                                 boolean enableSuppression,
                                 boolean writableStackTrace)

Constructs a new throwable with the specified detail message, cause, suppression enabled or disabled, and writable stack trace enabled or disabled. If suppression is disabled, getSuppressed() for this object will return a zero-length array and calls to addSuppressed(java.lang.Throwable)

Will discuss about the two methods which are used to find out the suppressed Exception details.

public final void addSuppressed(Throwable exception)
Appends the specified exception to the exceptions that were suppressed in order to deliver this exception. Since this method is final, so it can't be overridden. The suppression behavior is enabled by default. If you want to disable the suppression behavior, make the flag enableSuppression to false in the above constructor.

public final Throwable[] getSuppressed()
Returns an array containing all of the exceptions that were suppressed.

import java.io.*;

class MyResource{
 String name;

 void exception1(){
  throw new RuntimeException("I am exception 1");
 } 

 void exception2(){
  throw new RuntimeException("I am exception 2");
 } 

 void exception3(){
  throw new RuntimeException("I am exception 3");
 } 

 void exception4(){
  throw new RuntimeException("I am exception 4");
 }
}
  

import java.io.*;

class SuppressEx{

 static Throwable suppressed = new Throwable();

 static MyResource obj1 = new MyResource();

 static void throwFirst(){
  try{
   obj1.exception1();
  }
  catch(Exception e){
   suppressed.addSuppressed(e);
   throwSecond();
  }
 }

 static void throwSecond(){
  try{
   obj1.exception2();
  }
  catch(Exception e){
   suppressed.addSuppressed(e);
   throwThird();
  }
 }

 static void throwThird(){

  try{
   obj1.exception3();
  }
  catch(Exception e){
   suppressed.addSuppressed(e);
   obj1.exception4();
  }
 }

 public static void main(String args[])throws Exception{
  try{
   throwFirst();
  }
  catch(Exception e){
   System.out.println(e);
  }

  System.out.println("--------------------------");
  System.out.println("Suppressed Exceptions are");
  System.out.println("--------------------------");

  Throwable[] suppressedExceptions = suppressed.getSuppressed();

  for(int i=0; i < suppressedExceptions.length; i++){
   System.out.println(suppressedExceptions[i]);
  }
 }
}

Output
java.lang.RuntimeException: I am exception 4
--------------------------------
Suppressed Exceptions are
--------------------------------
java.lang.RuntimeException: I am exception 1
java.lang.RuntimeException: I am exception 2
java.lang.RuntimeException: I am exception 3



Suppressed Exceptions                                                 Exception Hierarchy                                                 Home

No comments:

Post a Comment