Throwable addSuppressed() method in Java with Examples

The addSuppressed?(Throwable exception) method of a Throwable class used to append the exception to the exceptions that were suppressed in order to deliver this exception. This method is a thread-safe method. This method is typically called by try-catch clause. The suppression behavior of a Throwable is enabled unless disabled via a constructor and When suppression is disabled, this method does nothing other than to validate its argument.

In default behavior of try-finally block, where we have two exceptions, the original exception is suppressed and exception from finally block is shown. In some situation, finally block is used in order to close the resource and in that situation we want to see the original exception, with some exceptions from the final block, that closed our resource and failed so we can add those exceptions which were suppressed via this method.

If there are multiple sibling exceptions and only one can be propagated, then this method can be used to propogate that method.



Syntax:

public final void addSuppressed?(Throwable exception)

Parameters: This method accepts only one parameter exception which we want to add as a suppressed exception.

Returns: This method does not returns anything.

Exceptions: This method throws following exceptions:

  • IllegalArgumentException : if exception is this throwable; a throwable cannot suppress itself.
  • NullPointerException : if exception is null.

Below programs illustrate the addSuppressed?() method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// the addSuppressed() Method.
  
import java.io.*;
  
class GFG {
  
    // Main Method
    public static void main(String[] args)
        throws Exception
    {
  
        try {
  
            testException();
        }
  
        catch (Throwable e) {
  
            // get StackTraceElements
            Throwable[] suppExe
                = e.getSuppressed();
  
            // print element of suppExe
            for (int i = 0; i < suppExe.length; i++) {
  
                System.out.println("Suppressed Exceptions:");
                System.out.println(suppExe[i]);
            }
        }
    }
  
    // method which throws Exception
    public static void testException()
        throws Exception
    {
  
        // creating a suppressed Exception
        Exception
            suppressed
            = new ArithmeticException();
  
        // creating a IOException object
        final Exception exe = new Exception();
  
        // adding suppressed Exception
        // using addSuppressed method
        exe.addSuppressed(suppressed);
  
        // throwing IOException
        throw exe;
    }
}

chevron_right


Output:

Suppressed Exceptions:
java.lang.ArithmeticException

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// the addSuppressed() Method.
  
import java.io.*;
  
class GFG {
  
    // Main Method
    public static void main(String[] args)
        throws Exception
    {
  
        try {
  
            testException();
        }
  
        catch (Throwable e) {
  
            // get StackTraceElements
            Throwable[] suppExe
                = e.getSuppressed();
  
            System.out.println("Suppressed Exceptions:");
  
            // print element of suppExe
            for (int i = 0; i < suppExe.length; i++) {
  
                System.out.println(suppExe[i]);
            }
        }
    }
  
    // method which throws Exception
    public static void testException()
        throws Exception
    {
  
        // creating a IOException object
        final Exception exe = new Exception();
  
        // adding suppressed Exception
        // using addSuppressed method
        exe.addSuppressed(new ArithmeticException());
        exe.addSuppressed(new IndexOutOfBoundsException());
        exe.addSuppressed(new ClassNotFoundException());
        exe.addSuppressed(new IOException());
  
        // throwing IOException
        throw exe;
    }
}

chevron_right


Output:

Suppressed Exceptions:
java.lang.ArithmeticException
java.lang.IndexOutOfBoundsException
java.lang.ClassNotFoundException
java.io.IOException

Example 3: To show IllegalArgumentException

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Exception IllegalArgumentException
// the addSuppressed() Method.
  
import java.io.*;
  
class GFG {
  
    // Main Method
    public static void main(String[] args)
        throws Exception
    {
  
        try {
  
            ArithmeticException
                e
                = new ArithmeticException();
            e.addSuppressed(e);
        }
  
        catch (Throwable e) {
            System.out.println("Exception:" + e);
        }
    }
}

chevron_right


Output:

Exception:java.lang.IllegalArgumentException:
 Self-suppression not permitted

Example 4: To show NullPointerException

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Exception NullPointerException
// the addSuppressed() Method.
  
import java.io.*;
  
class GFG {
  
    // Main Method
    public static void main(String[] args)
        throws Exception
    {
  
        try {
  
            ArithmeticException
                e
                = new ArithmeticException();
            e.addSuppressed(null);
        }
  
        catch (Throwable e) {
            System.out.println("Exception:" + e);
        }
    }
}

chevron_right


Output:

Exception:java.lang.NullPointerException:
 Cannot suppress a null exception.

References:
https://docs.oracle.com/javase/10/docs/api/java/lang/Throwable.html#addSuppressed(java.lang.Throwable)



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.