In Scala, can we add exception throw in a finally block back to the original exception as an suppressed exception?
In Java, if we hit an exception in the try block and then another exception in the finally block, the second exception will be added back to the first exception as a suppressed exception. Therefore, the second exception would not mask the first exception and we still can analyse what happened via checking its suppressed exception.
import java.util.stream.Stream;
class Scratch {
static class MyCloseable implements AutoCloseable {
@Override
public void close() throws Exception {
throw new Exception("FROM CLOSE METHOD");
}
}
public static void main(String[] args) {
try {
try (final MyCloseable closeable = new MyCloseable()){
throw new Exception("From Try Block");
}
} catch (Throwable t) {
System.out.println(t);
Stream.of(t.getSuppressed()).forEach(System.out::println);
}
}
}
would throw exceptions
java.lang.Exception: From Try Block
java.lang.Exception: FROM CLOSE METHOD
However, it seems that Scala simply rethrows the second exception (throwing from finally block) and ignore the first exception (throwing from the try block).
try {
try {
throw new Exception("From Try Block")
} finally {
throw new Exception("From Final Block")
}
} catch {
case e => e :: e.getSuppressed().toList
}
The above code will simply return only the second exception (thrown from final block). However, I would like to have a way to get both exception.
Anyway to make above code in a better way?