4

I have a code which generates 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?

moonwave99
  • 21,957
  • 3
  • 43
  • 64
gurehbgui
  • 14,236
  • 32
  • 106
  • 178
  • 1
    See http://stackoverflow.com/questions/1888602/why-is-java-lang-outofmemoryerror-java-heap-space-not-caught – devnull May 21 '13 at 17:06
  • PS: You should just print the stack trace (`e.printStackTrace()`), or if you're against that, use `System.err.println(e)` – Steve P. May 21 '13 at 17:06

1 Answers1

14

Because that's not an Exception but an Error.

You could catch it as a Throwable

} catch (Throwable e) {

or specifically as Error

} catch (Error e) {

But there's not a lot you can do with such an error, apart trying to close the application in the cleanest way you can.

From the javadoc :

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758