posted 22 years ago
If you change the code to catch (Throwable e), will the error be caught in 1.4.1?
I wondered that to, and just tested (on XP, using 1.4.1). Nope. If I run from TextPad, I get a message "Tool completed with exit code 128". If I run from the command prompt, or from Eclipse, it just terminates with no special message after approximately 4000 times through the loop (exact number varies). I also added a print statment after main to make sure it wasn't somehow exiting normally after the catch - no luck.
Shouldn't I have got an OutOfMemoryError?
Technically according to the API, OOME indicates the machine can't allocate memory for an Object - I.e. memory on the heap. What we have here is consuming memory on the stack. So I guess it makes some sense OOME is not thrown, but I'm not sure what the "right" response is then. Some sort of error seems appropriate. Very annoying.
I'm going to make fun of you for even suggesting to catch an Error!
Why? Sometimes it's the best thing to do. It may be risky, sure, but we are debugging after all. The program is already dying, so we're not likely to make things much worse. And even in production code catching an error may be appropriate when dealing with specific types of errors. E.g. catching an OutOfMemoryError may be OK if you can immediately free up some memory in response - something GC couldn't get to.
[ November 07, 2002: Message edited by: Jim Yingst ]
"I'm not back." - Bill Harding, Twister