The getSuppressed() method of Throwable class used to return an array containing all of the exceptions that were suppressed to deliver this exception typically this suppression done by the try-with-resources statement. In order to deliver Exception If no exceptions were suppressed or suppression is disabled, an empty array of suppressed exception is returned. Any changes to the returned array do not affect future calls to this method.Â
Syntax:
public final Throwable[] getSuppressed()
Parameters: This method does not accepts anything as a parameter.Â
Returns: This method returns return an array containing all of the exceptions that were suppressed.Â
Below programs illustrate the getSuppressed() method of Throwable class:Â
Example 1:Â
Java
// Java program to demonstrate// the getSuppressed() Method.Â
import java.io.*;Â
class GFG {Â
    // Main Method    public static void main(String[] args)        throws Exception    {Â
        try {Â
            testException1();        }Â
        catch (Throwable e) {Â
            // get Suppressed exception            // using getSuppressed() method            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 testException1()        throws Exception    {Â
        // creating a suppressed Exception        Exception            suppressed            = new ArrayIndexOutOfBoundsException();Â
        // creating a IOException object        final IOException ioe = new IOException();Â
        // adding suppressed Exception        ioe.addSuppressed(suppressed);Â
        // throwing IOException        throw ioe;    }} |
Suppressed Exceptions: java.lang.ArrayIndexOutOfBoundsException
Example 2:Â
Java
// Java program to demonstrate// the getSuppressed() Method.Â
import java.io.*;Â
class GFG {Â
    // Main Method    public static void main(String[] args)        throws Exception    {        try {Â
            // add the numbers            addPositiveNumbers(2, -1);        }Â
        catch (Throwable e) {Â
            // get Suppressed exception            // using getSuppressed() method            Throwable[] suppExe                = e.getSuppressed();Â
            // print element of suppExe            System.out.println("Suppressed Exception Array"                               + " length = "                               + suppExe.length);        }    }Â
    // method which adds two positive number    public static void addPositiveNumbers(int a, int b)        throws Exception    {Â
        // if Numbers are Positive        // then add or throw Exception        if (a < 0 || b < 0) {Â
            throw new Exception("Numbers are not Positive");        }Â
        else {Â
            System.out.println(a + b);        }    }} |
Suppressed Exception Array length = 0
References: https://docs.oracle.com/javase/10/docs/api/java/lang/Throwable.html#getSuppressed()
