When I run the code below, an OutOfMemoryError is thrown, but is not caught, despite the fact that there are clauses that should catch it:
public class SomeClass {
public static void main(String[] args) {
try {
//Allocate a huge amount of memory here
} catch (OutOfMemoryError oome) {
System.out.println("OutOfMemoryError=<" + oome + ">");
} catch (Error err) {
System.out.println("Error=<" + err + ">");
} catch (Throwable t) {
System.out.println("Throwable=<" + t + ">");
} finally {
System.out.println("'Finally' clause triggered");
}
}
}
The output is as follows:
'Finally' clause triggered
Exception in thread "main"
Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "main"
The fact that the exception is not caught makes no sense to me. The OutOfMemoryError documentation confirms that the exception should be caught by either Throwable, Error, or OutOfMemoryError. (Note that the "java.lang." specifier does not affect the behavior.)
All the other questions that I've seen here on StackOverflow along the lines of "Why isn't my OutOfMemoryError exception being caught?" were posted by people who were only catching Exception objects, not Error or Throwable.
Any idea as to what's going on?
Inside of your exception handler you're trying to allocate the string "OutOfMemoryError=<" + oome + ">"
, but you're out of memory and so this is probably going to throw another OutOfMemoryError
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With