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.
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:
Suppressed Exceptions: java.lang.ArithmeticException
Suppressed Exceptions: java.lang.ArithmeticException java.lang.IndexOutOfBoundsException java.lang.ClassNotFoundException java.io.IOException
Example 3: To show IllegalArgumentException
Exception:java.lang.IllegalArgumentException: Self-suppression not permitted
Example 4: To show NullPointerException
Exception:java.lang.NullPointerException: Cannot suppress a null exception.
- Throwable getLocalizedMessage() method in Java with Examples
- Throwable getCause() method in Java with Examples
- Throwable getMessage() method in Java with Examples
- Throwable initCause() method in Java with Examples
- Throwable toString() method in Java with Examples
- Throwable getStackTrace() method in Java with Examples
- Throwable printStackTrace() method in Java with Examples
- Throwable getSuppressed() method in Java with Examples
- Throwable setStackTrace() method in Java with Examples
- Throwable fillInStackTrace() method in Java
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.