Suppressed Exceptions Java Example - Java @ Desk

Thursday, July 31, 2014

Suppressed Exceptions Java Example

Suppressed Exceptions Java Example

Also, now with Java 7, exceptions are being suppressed rather than masked. Only one exception can be thrown at a time, meaning that even correct code misses information while handling exceptions. Developers lose significant time debugging when a main exception is masked by a further exception being thrown while closing a resource. Because exception masking is such an important problem in practice, Java SE 7 extends exceptions so that "suppressed" exceptions can be attached to primary exceptions. What we called a "masked" exception previously is actually an exception to be suppressed and attached to a primary exception.

The extensions to java.lang.Throwable are as follows:

1) public final void addSuppressed(Throwable exception) appends a suppressed exception to another one, so as to avoid exception masking.

2) public final Throwable[] getSuppressed() gets the suppressed exceptions that were added to an exception.

Following is the example to demonstrate the suppressed exception in Automatic Resource Management in Java 7 and how is that helpful

public class CustomConnectionImpl implements AutoCloseable {


 @Override
    public void close() throws MyException1{
        System.out.println("Closing new connection()");
        throw new MyException1("close");
    }
    
    public void open() throws MyException1 {
        System.out.println("Opening new connection()");
        throw new MyException1("Exception while opening");
        
    }
    
    public static void main(String[] args) {
        try (CustomConnectionImpl autoClose = new CustomConnectionImpl()) {
            autoClose.open();
            
        } catch (MyException1 e) {
            e.printStackTrace();
        }
    }
}
    
    class MyException1 extends Exception {
        
        public MyException1() {
            super();
        }
        
        public MyException1(String message) {
            super(message);
        }
    }


Generates the below output :



Opening new connection()
Closing new connection()
com.java7.coin.trycatch.MyException1: Exception while opening
 at com.java7.coin.trycatch.CustomConnectionImpl.open(CustomConnectionImpl.java:14)
 at com.java7.coin.trycatch.CustomConnectionImpl.main(CustomConnectionImpl.java:20)
 Suppressed: com.java7.coin.trycatch.MyException1: close
  at com.java7.coin.trycatch.CustomConnectionImpl.close(CustomConnectionImpl.java:9)
  at com.java7.coin.trycatch.CustomConnectionImpl.main(CustomConnectionImpl.java:22)


Following is the example to demonstrate the suppressed exception without using ARM for better debugging of expressions

public class CustomConnectionImpl implements AutoCloseable {
 public static void runWithSupressed () throws MyException1 {
  CustomConnectionImpl autoClose = new CustomConnectionImpl();
  MyException1 myException = null;
  try {
   autoClose.open();
  } catch (MyException1 e) {
   myException = e;
   throw e;
  } finally {
   if (myException != null) {
    try {
     autoClose.close();
    } catch (Throwable t) {
     myException.addSuppressed(t);
    }
   } else {
    autoClose.close();
   }
  }
 }

 @Override
 public void close() throws MyException1{
  System.out.println("Closing new connection()");
  throw new MyException1("close");
 }


 public void open() throws MyException1 {
  System.out.println("Opening new connection()");
  throw new MyException1("Exception while opening");
 }

 public static void main(String[] args) {
  try {
   runWithSupressed();        
  } catch (Throwable t) {
   t.printStackTrace();
  }
 }
}

class MyException1 extends Exception {
 public MyException1() {
  super();
 }

 public MyException1(String message) {
  super(message);
 }
}
}


Generates the below output:

Opening new connection()
Closing new connection()
com.fundamentals.exceptionhandling.MyException1: Exception while opening
at com.fundamentals.exceptionhandling.CustomConnectionImpl.open(CustomConnectionImpl.java:35)
at com.fundamentals.exceptionhandling.CustomConnectionImpl.runWithSupressed(CustomConnectionImpl.java:10)
at com.fundamentals.exceptionhandling.CustomConnectionImpl.main(CustomConnectionImpl.java:42)
Suppressed: com.fundamentals.exceptionhandling.MyException1: close
at com.fundamentals.exceptionhandling.CustomConnectionImpl.close(CustomConnectionImpl.java:30)
at com.fundamentals.exceptionhandling.CustomConnectionImpl.runWithSupressed(CustomConnectionImpl.java:17)
... 1 more


Thus demystifying the syntactic sugar of the ARM says that is much more beyond the expression. Moreover, it helps us to worry about what to do with resources rather than management of it. Using it not only simplifies the code, but also eradicates our nasty and redundant code, making the business logic more readable. Also, with the help of suppressed exception it helps us to debug our application correctly.

This post is written by Dipika Mulchandani. She is a freelance writer, loves to explore latest features in Java technology.





No comments:

Post a Comment