Java OutOfMemoryError is not caught by sentences that catch Error and Throwable

When I run the code below, an OutOfMemoryError is raised, but it is not caught, despite the fact that there are sentences 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 must be caught by either Throwable, Error, or OutOfMemoryError. (Note that the qualifier β€œjava.lang.” Does not affect behavior.)

All other questions that I saw here in StackOverflow line by line "Why is it possible that OutOfMemoryError Out is thrown out?" were sent by people who only caught Exception objects, not Error or Throwable.

Any idea on what's going on?

+4
source share
5 answers

Inside your exception handler, you are trying to highlight the line "OutOfMemoryError=<" + oome + ">" , but you have lost memory, and so this is probably going to throw another OutOfMemoryError

+16
source

You should have provided a complete trace of the OOME stack. It can be easily thrown out of the original OOME handler by masking it.

You also could not show the code that allocates a huge amount of memory. If you make this selection in one huge block, then the attempt to allocate will fail in general, leaving a lot of free memory. So try with this code in a try block:

 long[][] ary = new long[Integer.MAX_VALUE][Integer.MAX_VALUE]; 

I made a sample at Ideone.com that you can try.

+12
source

I think this is because you are allocating more memory inside the catch and throwing another OutOfMemoryException.

As stated in the comment on the question:

Try out the Zim-Zam O'Pootertoot idea and remove the β€œ+ err” and execute as in the end and see what results you get.

0
source

I can not reproduce the problem described in Oracle Java 8.

To allocate huge memory, I simply define an array of size Integer.MAX_VALUE, and I get this exception:

 OutOfMemoryError=<java.lang.OutOfMemoryError: Requested array size exceeds VM limit> 'Finally' clause triggered 

Try recompiling and please confirm that you still have this problem with Oracle Java 8. Please post the exact statements you commented on.

0
source

here you will find more information here Capture java.lang.OutOfMemoryError? as well as http://www.onjava.com/pub/a/onjava/2001/08/22/optimization.html

The problem in your code is how the JVM handles OOM exceptions. I think the thread is killed (in this case the main one), so your catch command is not available.

-2
source

Source: https://habr.com/ru/post/1493235/


All Articles