Suppressed exceptions in java 7

Suppressed exceptions, as name suggest, are exceptions thrown in the code but were ignored somehow. If you remember try-catch-finally block execution sequence and how they return any value or exceptions, you will recall that exceptions thrown in finally block are suppressed is exception is thrown in try block also. Before java 7, you was informed about these exceptions by logging if implemented, but you didn’t have any control over these types of exceptions once finally block is over.

Well, with new features in java 7 you got control over these suppressed exceptions as well. How? Lets see !!

Sections in this post

What are suppressed exceptions?
Example usage
Demonstration in different scenarios

What are suppressed exceptions?

In java 7, perhaps the most common use case for encountering suppressed exceptions is when a try-with-resources statement (which is the one type of try that does not require a catch or finally clause according to Java Language Specification Java SE 7 Edition Section 14.20) encounters an exception within the try block and then encounters another exception in implicitly trying to close the related resource. Because multiple exceptions may occur while closing AutoCloseable resources, additional exceptions are attached to a primary exception as suppressed exceptions.

A new constructor and two new methods were added to the Throwable class (parent of Exception and Error classes) in JDK 7. These are as below:

Throwable.getSupressed(); // Returns Throwable[]
Throwable.addSupressed(aThrowable);

Example scenario

For example while writing to output stream, an exception can be thrown from the try block, and up to two exceptions can be thrown from the try-with-resources statement when it tries to close the stream. If an exception is thrown from the try block and one or more exceptions are thrown from the try-with-resources statement, then those exceptions thrown from the try-with-resources statement are suppressed, and the exception thrown by the block is the one that is thrown by the closeStream() method.

You can retrieve these suppressed exceptions by calling the Throwable.getSuppressed method from the exception thrown by the try block.

Demonstration in different scenarios

For example purpose, I am writing an auto closeable resource (i.e. DirtyResource.java) which throws exceptions whether we try to access it or close it. This way we will be able to see different behavior when accessed in different manner.

public class DirtyResource implements AutoCloseable
{
	/**
	 * Need to call this method if you want to access this resource
	 * @throws RuntimeException no matter how you call this method
	 * */
	public void accessResource()
	{
		throw new RuntimeException("I wanted to access this resource. Bad luck. Its dirty resource !!!");
	}

	/**
	 * The overridden closure method from AutoCloseable interface
	 * @throws Exception which is thrown during closure of this dirty resource
	 * */
	@Override
	public void close() throws Exception
	{
		throw new NullPointerException("Remember me. I am your worst nightmare !! I am Null pointer exception !!");
	}
}

Scenario 1: As it was prior to suppressed exceptions support

package com.howtodoinjava.demo.core;

import static java.lang.System.err;

public class SuppressedExceptionDemoWithTryFinallyPrevious
{
	/**
    * Executable member function demonstrating suppressed exceptions
    * One exception is lost if not added in suppressed exceptions list
    */
	public static void memberFunction() throws Exception
	{
		DirtyResource resource= new DirtyResource();
		try
	    {
	    	  resource.accessResource();
	    }
		finally
		{
			resource.close();
		}
	}

	public static void main(String[] arguments) throws Exception
   {
      try
      {
    	  memberFunction();
      }
      catch(Exception ex)
      {
    	  err.println("Exception encountered: " + ex.toString());
    	  final Throwable[] suppressedExceptions = ex.getSuppressed();
    	  final int numSuppressed = suppressedExceptions.length;
    	  if (numSuppressed > 0)
    	  {
    		  err.println("tThere are " + numSuppressed + " suppressed exceptions:");
	    	  for (final Throwable exception : suppressedExceptions)
	    	  {
	    		  err.println("tt" + exception.toString());
	    	  }
    	  }
      }
   }
}

Output:

Exception encountered: java.lang.NullPointerException: Remember me. I am your worst nightmare !! I am Null pointer exception !!

As you can see that only one exception was captured and second RuntimeException was suppressed.

Scenario 2: As it is after the suppressed exceptions support in java 7

package com.howtodoinjava.demo.core;

import static java.lang.System.err;

public class SuppressedExceptionDemoWithTryFinallyNew
{
	/**
    * Executable member function demonstrating suppressed exceptions
    * Suppressed expression is added back in primary exception
    */
	public static void memberFunction() throws Exception
	{
		Throwable th = null;
		DirtyResource resource= new DirtyResource();
		try
	    {
	    	  resource.accessResource();
	    }
		catch(Exception e)
		{
			th = e;
		}
		finally
		{
			try
			{
				resource.close();
			}
			catch(Exception e)
			{
				if(th != null)
				{
					e.addSuppressed(th); //Add to primary exception
					throw e;
				}
			}
		}
	}
   /**
    * Executable function demonstrating suppressed exceptions.
    */
   public static void main(String[] arguments) throws Exception
   {
      try
      {
    	  memberFunction();
      }
      catch(Exception ex)
      {
    	  err.println("Exception encountered: " + ex.toString());
    	  final Throwable[] suppressedExceptions = ex.getSuppressed();
    	  final int numSuppressed = suppressedExceptions.length;
    	  if (numSuppressed > 0)
    	  {
    		  err.println("tThere are " + numSuppressed + " suppressed exceptions:");
	    	  for (final Throwable exception : suppressedExceptions)
	    	  {
	    		  err.println("tt" + exception.toString());
	    	  }
    	  }
      }
   }
}

Output:

Exception encountered: java.lang.NullPointerException: Remember me. I am your worst nightmare !! I am Null pointer exception !!
	There are 1 suppressed exceptions:
		java.lang.RuntimeException: I wanted to access this resource. Bad luck. Its dirty resource !!!

Here, in catch block we are able to access both exception. One as primary exception and second as suppressed exception.

Scenario 3: Using with try-with-resource block in a member function and catching the exception

package com.howtodoinjava.demo.core;

import static java.lang.System.err;

public class SuppressedExceptionDemoWithTryCatch
{
	public static void memberFunction() throws Exception
	{
		try (DirtyResource resource= new DirtyResource())
	      {
	    	  resource.accessResource();
	      }
	}
   /**
    * Executable member function demonstrating suppressed exceptions using try-with-resources
    */
   public static void main(String[] arguments) throws Exception
   {
      try
      {
    	  memberFunction();
      }
      catch(Exception ex)
      {
    	  err.println("Exception encountered: " + ex.toString());
    	  final Throwable[] suppressedExceptions = ex.getSuppressed();
    	  final int numSuppressed = suppressedExceptions.length;
    	  if (numSuppressed > 0)
    	  {
    		  err.println("tThere are " + numSuppressed + " suppressed exceptions:");
	    	  for (final Throwable exception : suppressedExceptions)
	    	  {
	    		  err.println("tt" + exception.toString());
	    	  }
    	  }
      }
   }
}

Output:

Exception encountered: java.lang.RuntimeException: I wanted to access this resource. Bad luck. Its dirty resource !!!
	There are 1 suppressed exceptions:
		java.lang.NullPointerException: Remember me. I am your worst nightmare !! I am Null pointer exception !!

Great !! We are able to see both exceptions when using try-with-resource in member function.

Scenario 4: Using with default try-with-resource block

package com.howtodoinjava.demo.core;

public class SuppressedExceptionDemoWithTryWithResource
{
   /**
    * Demonstrating suppressed exceptions using try-with-resources
    */
   public static void main(String[] arguments) throws Exception
   {
      try (DirtyResource resource= new DirtyResource())
      {
    	  resource.accessResource();
      }
   }
}

Output:

Exception in thread "main" java.lang.RuntimeException: I wanted to access this resource. Bad luck. Its dirty resource !!!
	at DirtyResource.accessResource(DirtyResource.java:9)
	at SuppressedExceptionDemoWithTryWithResource.main(SuppressedExceptionDemoWithTryWithResource.java:12)
	Suppressed: java.lang.NullPointerException: Remember me. I am your worst nightmare !! I am Null pointer exception !!
		at DirtyResource.close(DirtyResource.java:19)
		at SuppressedExceptionDemoWithTryWithResource.main(SuppressedExceptionDemoWithTryWithResource.java:13)

Well, this is really great to see the output with complete information with suppressed exceptions as well.

Happy Learning !!

4 thoughts on “Suppressed exceptions in java 7”

  1. Hi Lokesh ,
    I am bit confused with lines mentioned below . Please correct me If I’m going to wrong path. Lines are —
    ” If an exception is thrown from the try block and one or more exceptions are thrown from the try-with-resources statement, then those exceptions thrown from the try-with-resources statement are suppressed, and the exception thrown by the block is the one that is thrown by the closeStream() method.”

    By the help of these lines you are trying to say that ultimately exception of TRY block will be thrown and other exceptions occurred during closing the resources will be suppressed…… Right ??

Note:- In comment box, please put your code inside [java] ... [/java] OR [xml] ... [/xml] tags otherwise it may not appear as intended.

Want to ask any question? Or suggest anything?