Why is my exception not caught?

I have code that throws the following exception:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at edu.createArrays(ExhaustivePCFGParser.java:2188) at edu.considerCreatingArrays(ExhaustivePCFGParser.java:2158) at edu.ExhaustivePCFGParser.parse(ExhaustivePCFGParser.java:339) at edu.LexicalizedParserQuery.parse(LexicalizedParserQuery.java:247) at edu.LexicalizedParser.apply(LexicalizedParser.java:283) at current.Gen.textParsen(Gen.java:162) at current.Gen.gen(Gen.java:90) at current.Foo.main(Foo.java:120) 

I tried to catch it with the following code:

 try { // this is the line which cause the exception LinkedList<Foo> erg = Gen.gen(rev); } catch (Exception e) { System.out.println("exception: out of memory"); } 

but when I run the code, the exception is not caught. How to solve this?

+4
source share
1 answer

Because it is not an Exception , but an Error .

You can catch it like a Throwable

 } catch (Throwable e) { 

or specifically as Error

 } catch (Error e) { 

But you cannot do so much with such a mistake, trying to close the application in the cleanest way.

From javadoc :

A bug is a Throwable subclass that indicates serious problems that a reasonable application should not try to catch

+14
source

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


All Articles