Open In App

Throwable addSuppressed() method in Java with Examples

Last Updated : 29 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 propagate 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: 
 

Java




// 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;
    }
}


Output: 

Suppressed Exceptions:
java.lang.ArithmeticException

 

Example 2:
 

Java




// 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;
    }
}


Output: 

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

 

Example 3: To show IllegalArgumentException
 

Java




// 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);
        }
    }
}


Output: 

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

 

Example 4: To show NullPointerException
 

Java




// 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);
        }
    }
}


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)
 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads