Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Java RuntimeException enableSuppression parameter do ?

I don't know how to say that I've been working with Java for 4 years now and for the first time I encounter this parameter enableSuppression can someone explain what does it do and when to enable/disable it ?

like image 212
Adelin Avatar asked Sep 07 '15 12:09

Adelin


People also ask

What does a runtime exception do in Java?

The Runtime Exception is the parent class in all exceptions of the Java programming language that are expected to crash or break down the program or application when they occur. Unlike exceptions that are not considered as Runtime Exceptions, Runtime Exceptions are never checked.

Should I extend exception or RuntimeException?

You just need to extend Exception for a custom checked exception, or RuntimeException if it's a custom unchecked exception. In addition to that, you should follow a few best practices. They make your code easier to read and your API easier to use.

Is it good practice to throw RuntimeException?

Generally speaking, do not throw a RuntimeException or create a subclass of RuntimeException simply because you don't want to be bothered with specifying the exceptions your methods can throw.

What happens when we throw runtime exception?

3) Throwing runtime exceptions forces the users of the class throwing the exception to walk through the source code to debug and see why the exception is thrown. This can only be avoided if the Javadoc of the runtime exception happens to be excellently documented which I find is never a case.


1 Answers

enableSuppression is a parameter in the constructor of Throwables (including Exception)

It determines whether or not suppression is enabled.

From Javadocs:

The suppression behavior is enabled unless disabled via a constructor.

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.

like image 126
Amila Avatar answered Sep 16 '22 11:09

Amila