CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > Suppressed Exceptions in Java

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor
  • Home

Suppressed Exceptions in Java

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 · 3 min read · 0 Comments
Share: in X

Exceptions in Java are errors that disrupt normal program flow. But what if multiple exceptions occur at once? This is where suppressed exceptions come into play. Let’s explore how Java handles them, with examples and simple explanations.

Table of Contents

  • What Are Suppressed Exceptions?
  • Example 1: The Problem with try-catch-finally
    • Code Example:
  • Java 7+ Solution: try-with-resources
    • How Suppression Works:
  • Example 2: try-with-resources
    • Step 1: Define a Resource
    • Step 2: Use try-with-resources
  • How to Catch All Exceptions
  • Benefits of Suppressed Exceptions
  • Potential Problems
  • Summary
  • FAQs
    • How do I access suppressed exceptions without try-with-resources?
    • Are suppressed exceptions only for AutoCloseable resources?
    • Can a suppressed exception become the primary one?
    • Does Java 8 handle suppressed exceptions differently?

What Are Suppressed Exceptions?

Suppressed exceptions occur when multiple exceptions are thrown, but only one is reported. The others are “suppressed” (attached to the primary exception). This often happens in:

  • try-catch-finally blocks (pre-Java 7).
  • try-with-resources blocks (Java 7+).

Example 1: The Problem with try-catch-finally

In older Java versions, if exceptions occur in both the try and finally blocks, the finally exception overrides the original one.

Code Example:

import java.io.*;

public class Main {
    public static void main(String[] args) {
        try {
            System.out.println("Inside try block");
            throw new IOException("Error in try"); // Primary exception
        } finally {
            System.out.println("Inside finally block");
            throw new NullPointerException("Error in finally"); // Overrides the original!
        }
    }
}
surpressed exceptions before java 7

Output:

Inside try block
Inside finally block
Exception in thread "main" java.lang.NullPointerException: Error in finally
        at Main.main(Main.java:10)

Problem: The IOException from the try block is lost because the finally block’s exception takes over.

Java 7+ Solution: try-with-resources

Java 7 introduced try-with-resources and the addSuppressed() method. Now, if multiple exceptions occur, the primary exception is thrown, and others are attached as suppressed.

How Suppression Works:

  1. Primary Exception: The first exception thrown (e.g., in the try block).
  2. Suppressed Exceptions: Subsequent exceptions (e.g., in close() method of resources).

Example 2: try-with-resources

Let’s create a custom resource and see suppression in action.

Step 1: Define a Resource

class MyResource implements AutoCloseable {
    @Override
    public void close() throws Exception {
        throw new IllegalStateException("Error closing resource"); // Suppressed!
    }
}

Step 2: Use try-with-resources

import java.io.*;
public class Main {
    public static void main(String[] args) {
        try (MyResource resource = new MyResource()) {
            throw new IOException("Error in try block"); // Primary exception
        } catch (Exception e) {
            System.out.println("Primary Exception: " + e.getMessage());
            for (Throwable suppressed : e.getSuppressed()) {
                System.out.println("Suppressed: " + suppressed.getMessage());
            }
        }
    }
}
Suppressed Exceptions after java 7

Output:

Primary Exception: Error in try block  
Suppressed: Error closing resource  

Key Points:

  • The IOException is the primary exception.
  • The IllegalStateException from close() is suppressed and attached.

How to Catch All Exceptions

Use getSuppressed() to retrieve suppressed exceptions:

try {
    // Code that throws exceptions
} catch (Exception e) {
    System.out.println("Main Exception: " + e.getMessage());
    Throwable[] suppressed = e.getSuppressed();
    for (Throwable s : suppressed) {
        System.out.println("Suppressed: " + s.getMessage());
    }
}

Benefits of Suppressed Exceptions

  1. No Lost Exceptions: All errors are recorded.
  2. Better Debugging: See the full chain of failures.
  3. Cleaner Code: No need for nested try-catch blocks.

Potential Problems

  1. Complexity: Beginners might overlook suppressed exceptions.
  2. Java Version Dependency: Requires Java 7+.
  3. Manual Handling: You must explicitly call getSuppressed().

Summary

  • Pre-Java 7: Exceptions in finally override the original.
  • Java 7+: try-with-resources keeps all exceptions (primary + suppressed).
  • Use getSuppressed(): To retrieve attached exceptions.

By understanding suppressed exceptions, you can debug complex issues and write robust Java code!

FAQs

How do I access suppressed exceptions without try-with-resources?

Use Throwable.addSuppressed() and getSuppressed() manually.

Are suppressed exceptions only for AutoCloseable resources?

No, but they’re most common in try-with-resources.

Can a suppressed exception become the primary one?

No. The first exception in the try block is primary.

Does Java 8 handle suppressed exceptions differently?

No—the behavior is consistent from Java 7 onward.

Related Posts:

  • What is Try-With-Resources in Java? A Complete Guide
  • Exception Handling Keywords in Java
  • User Defined Exception in Java
  • Top 10 Common Java Errors
  • Control Statements in Java
  • Exception Handling in Java
Tags:exceptionjavalanguage-fundamentals
Was this article helpful?
← Previous ArticleWhat is Try-With-Resources in Java? A Complete Guide
Next Article →Convert List to Comma Separated String in Java

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap