0

Is memory out of bound exception or error? We usually get this during project deployment on the server. It might be a basic question. I googled it but I could not find relevant answer so posting here.

Error I got:

Invocation of init method failed; nested exception is java.lang.OutOfMemoryError: allocLargeObjectOrArray - Object size: 8216, Num elements: 2049

And how can we handle this?

Wtower
  • 18,848
  • 11
  • 103
  • 80
manohar e
  • 327
  • 3
  • 6
  • 16

1 Answers1

3

java.lang.OutOfMemoryError extends java.lang.Error and not java.lang.Exception

catching Exception you will miss it

try{
....
}catch(Exception ex){
 //will not catch OutOfMemoryError, since it does not extend Exception
}

catching Throwable, you will hit'em both..

try{
....
}catch(Throwable ex){
 //will catch both Exception and OutOfMemoryError, they both extend this
}

Whether it is good or not to catch Throwable is another question see this Is it a bad practice to catch Throwable? (Thanks to @Dawnkeeper for link)

Community
  • 1
  • 1
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
  • 1
    just adding that catching Throwables might not really help you as Errors tend to not be recoverable. see [Here](http://stackoverflow.com/questions/6083248/is-it-a-bad-practice-to-catch-throwable) – Dawnkeeper Feb 02 '16 at 12:31