Throwable addSuppressed() method in Java with Examples
Last Updated :
29 May, 2021
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
import java.io.*;
class GFG {
public static void main(String[] args)
throws Exception
{
try {
testException();
}
catch (Throwable e) {
Throwable[] suppExe
= e.getSuppressed();
for ( int i = 0 ; i < suppExe.length; i++) {
System.out.println( "Suppressed Exceptions:" );
System.out.println(suppExe[i]);
}
}
}
public static void testException()
throws Exception
{
Exception
suppressed
= new ArithmeticException();
final Exception exe = new Exception();
exe.addSuppressed(suppressed);
throw exe;
}
}
|
Output:
Suppressed Exceptions:
java.lang.ArithmeticException
Example 2:
Java
import java.io.*;
class GFG {
public static void main(String[] args)
throws Exception
{
try {
testException();
}
catch (Throwable e) {
Throwable[] suppExe
= e.getSuppressed();
System.out.println( "Suppressed Exceptions:" );
for ( int i = 0 ; i < suppExe.length; i++) {
System.out.println(suppExe[i]);
}
}
}
public static void testException()
throws Exception
{
final Exception exe = new Exception();
exe.addSuppressed( new ArithmeticException());
exe.addSuppressed( new IndexOutOfBoundsException());
exe.addSuppressed( new ClassNotFoundException());
exe.addSuppressed( new IOException());
throw exe;
}
}
|
Output:
Suppressed Exceptions:
java.lang.ArithmeticException
java.lang.IndexOutOfBoundsException
java.lang.ClassNotFoundException
java.io.IOException
Example 3: To show IllegalArgumentException
Java
import java.io.*;
class GFG {
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
import java.io.*;
class GFG {
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)
Share your thoughts in the comments
Please Login to comment...