DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • SRE vs. DevOps
  • How To Become a 10x Dev: An Essential Guide
  • Implementing RBAC in Quarkus
  • Competing Consumers With Spring Boot and Hazelcast

Exception Handling With Try-With-Resource Suppressed Exceptions

Learn more about different forms of exception handling with the try-with-resource suppressed exception.

Anil Thirunagari user avatar by
Anil Thirunagari
·
Jan. 02, 20 · Tutorial
Like (4)
Save
Tweet
Share
8.85K Views

Join the DZone community and get the full member experience.

Join For Free

Person programming on laptop

Learn more about different forms of exception handling with the try-with-resource suppressed exception.

In this article, we are going to deal with multiple cases of exception handling with and without the try-with-resources feature in Java.  For details about try-with-resources, please refer to the Oracle docs. 

You may also like: 9 Best Practices to Handle Exceptions in Java

Below are the cases we will discuss:

1. Try and finally block without try-with-resource
2. Try and finally block with try-with-resource
3. Try, catch, and finally block without try-with-resource
4. Try, catch, and finally block with try-with-resource
5. Try, catch, and finally block with try-with-resource and suppress exceptions
6. Try, catch, and finally block with try-with-resource and suppress exceptions with multiple resources

The above exception cases are evaluated based on when the exception is thrown, both from a try block and while the resource is being closed (either in finally or in the auto-closable close method).

Case 1: Try and Finally Block Without Try-With-Resource

In this case, even though the exception is thrown by both: 

  •  resource.run()— line 9

  •  resource.close(); — line 12

The exception thrown by  resource.run() is suppressed and only the resource.close() exception is thrown.

package com.core.exception.trywithresource;

public class TryWithOutResources11 {

    public static void main(String[] args) throws Exception {

            OpenResource resource = new OpenResource();
            try {
                resource.run(); /*this throws a RunResourceException*/
            }finally {
                if(null!=resource) {
                    resource.close();
                }
                    System.out.println("I'm putting a sweater on, regardless. ");
            }
    }
}

/* **************** CONSOLE OUTPUT ****************
 * The resource is open.Exception in thread "main" 
The resource is having problem running.... :(
The resource is closed.
com.core.exception.trywithresource.CloseResourceException
        at com.core.exception.trywithresource.OpenResource.close(OpenResource.java:15)
        at com.core.exception.trywithresource.TryWithOutResources11.main(TryWithOutResources11.java:12)

 */ 



class OpenResource implements AutoCloseable {

        public OpenResource() throws Exception {
                System.out.println("The resource is open.");
        }
        public void run() throws Exception {
                System.out.println("The resource is having problem running.... :(");
                throw new RunResourceException();
        }

        public void close() throws Exception {
                System.out.println("The resource is closed.");
                throw new CloseResourceException(); /* throwing CloseException */
        }
}


class OpenException extends Exception {}
class RunResourceException extends Exception {}
class CloseResourceException extends Exception {}


Case 2: Try and Finally Block With Try-With-Resource

In this case, even though the exception is thrown by both: 

  • resource.run()

  • resource.close() 

The exception thrown by resource.close() is suppressed and only theresource.run() exception is thrown.

package com.core.exception.trywithresource;

public class TryWithResources12 {

    public static void main(String[] args) throws Exception {

            try ( OpenResource resource = new OpenResource() ) {
                resource.run(); /*this throws a RunResourceException*/
            }
            finally {
                    System.out.println("I'm putting a sweater on, regardless. ");
            }
    }
}

/* **************** CONSOLE OUTPUT ****************
The resource is open.Exception in thread "main" 
The resource is having problem running.... :(
The resource is closed.
I'm putting a sweater on, regardless. 
com.core.exception.trywithresource.RunResourceException
        at com.core.exception.trywithresource.OpenResource.run(OpenResource.java:10)
        at com.core.exception.trywithresource.TryWithResources12.main(TryWithResources12.java:8)
        Suppressed: com.core.exception.trywithresource.CloseResourceException
                at com.core.exception.trywithresource.OpenResource.close(OpenResource.java:15)
                at com.core.exception.trywithresource.TryWithResources12.main(TryWithResources12.java:9)
*/


Case 3: Try, Catch, and Finally Block Without Try-With-Resource 

In this case, even though the exception is thrown by both resource.run() and resource.close(). The catch block is handling the resource.run() exception, and finally needs to handle the resource.close() exception.

package com.core.exception.trywithresource;

public class TryWithOutResources13 {

    public static void main(String[] args) throws Exception {

            OpenResource resource = new OpenResource();

            try {
                resource.run(); /*this throws a RunResourceException*/
            }
            catch (Exception e) { 
                    System.out.println("Is there a draft? " + e.getClass());
            }
            finally {
                if(null!=resource) {
                    resource.close();
                }
                    System.out.println("I'm putting a sweater on, regardless. ");
            }
    }
}

/* **************** CONSOLE OUTPUT ****************
The resource is open.
The resource is having problem running.... :(
Is there a draft? class com.core.exception.trywithresource.RunResourceException
The resource is closed.
Exception in thread "main" com.core.exception.trywithresource.CloseResourceException
        at com.core.exception.trywithresource.OpenResource.close(OpenResource.java:15)
        at com.core.exception.trywithresource.TryWithOutResources13.main(TryWithOutResources13.java:17) */


Case 4: Try, Catch, and Finally Block With Try-With-Resource

In this case, even though the exception is thrown by both resource.run() and resource.close(), the catch block is handling the resource.run() exception and the exception thrown by resource.close() is suppressed.

package com.core.exception.trywithresource;

public class TryWithResources14 {

    public static void main(String[] args) throws Exception {

            try ( OpenResource resource = new OpenResource() ) {
                resource.run(); /*this throws a RunResourceException*/
            }
            catch (Exception e) { 
                    System.out.println("Is there a draft? " + e.getClass());
            }
            finally {
                    System.out.println("I'm putting a sweater on, regardless. ");
            }
    }
}

/* **************** CONSOLE OUTPUT ****************
The resource is open.
The resource is having problem running.... :(
The resource is closed.
Is there a draft? class com.core.exception.trywithresource.RunResourceException
I'm putting a sweater on, regardless.
 */


Case 5: Try, Catch, and Finally Block With Try-With-Resource and Suppressed Exceptions

In this case, even though the exception is thrown by both  resource.run() and  resource.close(), the catch block is handling the resource.run() exception thrown and the exception thrown by resource.close() is supressed.

To getSuppressed exception info, we need to use "e.getSuppressed()" in lines 12 and 14:

package com.core.exception.trywithresource;

public class TryWithResources15 {

    public static void main(String[] args) throws Exception {

            try ( OpenResource resource = new OpenResource() ) {
                resource.run(); /*this throws a RunResourceException*/
            }
            catch (Exception e) { 
                    System.out.println("Is there a draft? " + e.getClass());
                    int suppressedCount = e.getSuppressed().length;
                    for (int i=0; i<suppressedCount; i++){
                            System.out.println("Suppressed: " + e.getSuppressed()[i]);
                    }
            }
            finally {
                    System.out.println("I'm putting a sweater on, regardless. ");
            }
    }
}

/* **************** CONSOLE OUTPUT ****************
The resource is open.
The resource is having problem running.... :(
The resource is closed.
Is there a draft? class com.core.exception.trywithresource.RunResourceException
Suppressed: com.core.exception.trywithresource.CloseResourceException
I'm putting a sweater on, regardless. 

 */


According to the Oracle docs: 

 public final void addSuppressed(Throwable exception): Appends the specified exception to the exceptions that were suppressed in order to deliver this exception.
 public final Throwable[] getSuppressed(): Returns an array containing all of the exceptions that were suppressed, typically by the try-with-resources statement, in order to deliver this exception.
 protected Throwable(String message, Throwable cause,boolean enableSuppression, boolean writableStackTrace): Note that this constructor is duplicated in the java.lang.Exception class in Java 7, due the fact that constructors are not inherited by subclasses.
Other constructors of  Throwable treat suppression as being enabled. Subclasses of  Throwable should document any conditions under which suppression is disabled.

Disabling of suppression should only occur in exceptional circumstances where special requirements exist, such as a virtual machine reusing exception objects under low-memory situations. Circumstances where a given exception object is repeatedly caught and rethrown, such as to implement control flow between two sub-systems, is another situation where immutable throwable objects would be appropriate.

Case 6: Try, Catch, and Finally Block With Try-With-Resource and Suppress Exceptions With Multiple Resources

In this case, even though the exception is thrown by both resource.run() and  resource.close(), the catch block handles the resource.run() exception thrown and the exception thrown by resource.close()  is suppressed.

To getSuppressed exception info, we need to use "e.getSuppressed()" in lines 13 and 15.

In this case, two resources are used, i.e resource and  resource2. They will be closed first by  resource2 and then followed byresource. The getSuppressed method gives two exceptions, one for each resource close.

package com.core.exception.trywithresource;

public class TryWithResources16 {

    public static void main(String[] args) throws Exception {

            try ( OpenResource resource = new OpenResource();OpenResource2 resource2 = new OpenResource2() ) {
                resource.run(); /*this throws a RunResourceException*/
                resource2.run();
            }
            catch (Exception e) { 
                    System.out.println("Is there a draft? " + e.getClass());
                    int suppressedCount = e.getSuppressed().length;
                    for (int i=0; i<suppressedCount; i++){
                            System.out.println("Suppressed: " + e.getSuppressed()[i]);
                    }
            }
            finally {
                    System.out.println("I'm putting a sweater on, regardless. ");
            }
    }
}

/* **************** CONSOLE OUTPUT ****************
The resource is open.
The resource2 is open.
The resource is having problem running.... :(
The resource2 is closed.
The resource is closed.
Is there a draft? class com.core.exception.trywithresource.RunResourceException
Suppressed: com.core.exception.trywithresource.CloseResourceException
Suppressed: com.core.exception.trywithresource.CloseResourceException
I'm putting a sweater on, regardless. 

 */

class OpenResource2 implements AutoCloseable {

    public OpenResource2() throws Exception {
            System.out.println("The resource2 is open.");
    }
    public void run() throws Exception {
            System.out.println("The resource2 is having problem running.... :(");
            throw new RunResourceException();
    }

    public void close() throws Exception {
            System.out.println("The resource2 is closed.");
            throw new CloseResourceException(); /* throwing CloseException */
    }
}


That's it! We hope you enjoyed. Please leave any thoughts or questions in the comments below.

Further Reading

9 Best Practices to Handle Exceptions in Java

Java Exception Handling Interview Questions and Answers 

Java Exception Concept and How to Handle Them

Blocks

Opinions expressed by DZone contributors are their own.

Trending

  • SRE vs. DevOps
  • How To Become a 10x Dev: An Essential Guide
  • Implementing RBAC in Quarkus
  • Competing Consumers With Spring Boot and Hazelcast

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: