I need an expert to make me understand what Java Throwable's addSuppressed does? [duplicate]
Asked Answered
A

1

6

Possible Duplicate:
JDK 1.7 Throwable `addSuppressed()` method

So Java has a method in the Throwable

public final void addSuppressed(Throwable exception) 

And this is what it does:

Appends the specified exception to the exceptions that were suppressed in order to deliver this exception. This method is thread-safe and typically called (automatically and implicitly) by the try-with-resources statement.

..i'm puzzled at this , what is "specified exception to the exceptions that were suppressed in order to deliver this exception." ?

Artful answered 6/12, 2012 at 18:2 Comment(5)
Basically, the method just adds data to the "documentation" that goes with the exception. When you print out the stack trace of the exception (see printStackTrace()) there may be a list of "suppressed" exceptions. This adds to that list.Flickertail
@HotLicks - Ah OK I think I'm understanding this better . So there's a customized list of exceptions that you can suppress.. hmm let me study more. thanks!!Artful
@mtk - Thanks for that link - it is quite depth/specific I'm going to study it though.. thanks!!Artful
Adel, I don't fully understand it myself, but I think it's basically that a function will sometimes suppress several exceptions while handling one, and without this mechanism the information about those would be lost. It's not that you can specify a list of exceptions to automatically suppress.Flickertail
@HotLicks - Hmm yes, that makes sense; and I will keep studyin til I get it!Artful
A
2

There is a pretty good explanation in the docs: Throwable

The suppression behavior is enabled unless disabled via a constructor. When suppression is disabled, this method does nothing other than to validate its argument.

Note that when one exception causes another exception, the first exception is usually caught and then the second exception is thrown in response. In other words, there is a causal connection between the two exceptions. In contrast, there are situations where two independent exceptions can be thrown in sibling code blocks, in particular in the try block of a try-with-resources statement and the compiler-generated finally block which closes the resource. In these situations, only one of the thrown exceptions can be propagated. In the try-with-resources statement, when there are two such exceptions, the exception originating from the try block is propagated and the exception from the finally block is added to the list of exceptions suppressed by the exception from the try block. As an exception unwinds the stack, it can accumulate multiple suppressed exceptions.

An exception may have suppressed exceptions while also being caused by another exception. Whether or not an exception has a cause is semantically known at the time of its creation, unlike whether or not an exception will suppress other exceptions which is typically only determined after an exception is thrown.

Note that programmer written code is also able to take advantage of calling this method in situations where there are multiple sibling exceptions and only one can be propagated.

Alleyway answered 6/12, 2012 at 18:13 Comment(1)
Thank you i'm studying! !!! Any elaboration ?Artful

© 2022 - 2024 — McMap. All rights reserved.