Luke wrote:
> My application was throwing an OutOfMemoryError. While looking for the
> problem I was surprised to find that I could not catch the error and
> print the stack trace. My catch statement seemed to be ignored and I
> would only see a "java.lang.OutOfMemoryError" logged to the System.out
> stream.
You may have been misunderstanding what you saw. You *can* catch
OutOfMemoryError, either explicitly or by catching Error or Throwable.
You should not, however, expect to get a stack trace out of one. The VM
creates an OOME instance at startup to throw at need, because if it
really does run out of memory and need to throw one then it may not at
that point have enough memory to create a fresh one. If, as you say,
"java.lang.OutOfMemoryError" was being printed then that probably *was*
the stack trace.
> I was allocating large chunks of memory, so when the error happened I
> assumed that there would be enough memory left for my application to
--^^^^^^^
You are unwise to write programs that depend on unfounded assumptions.
It will bite you. Moreover, if your assumption is reasonable but wrong
then it may only bite you infrequently -- such as only after you ship
the product.
> handle the error. I'm still curious about the failure to catch the
> error.
It sounds to me like the error was caught, and you just didn't recognize it.
--
John Bollinger