Hello if and OutOfMemoryError is thrown the jvm and application stops ? I would like to know what would be the result of catching Throwable in the case where the bloc of code between try catch cause the jvm to throw a OutOfMemoryError ?
There was somebody on these boards about 2 months ago who had that problem with a large application with millions of objects in, and he managed to keep it running by setting a few million to null, so the garbage collector could delete them. If you don't manage something like that, your JVM will probably still go down.
Joined: Aug 11, 2007
Thank you for your answer, i actually wanted to know, what would be the purpose and the result of catching unchecked exceptions and errors having try catch bloc catching Throwable. Usually errors or unchecked exceptions are unrecoverable so what would be the result by catching it we let the application still running. I m working on a multithreaded application and i wasnt sure wether to catch Exceptions or Throwable or just checked exceptions.
The most common reason is probably to log it, perhaps adding some useful information, and maybe rethrowing it afterwards, especially if it's an Error.
When you do this, you can't be sure that any of the code in the catch block will really execute, because it's possible that the problem is so bad that your code will immediately throw another error and leave the catch block. But I usually figure it's better to try to log the problem and fail, rather than not even try.
Specific exceptions and errors may have specific workarounds (yes even Errors, sometimes at least) so you can catch them and handle them - but in this case you probably shouldn't catch Throwable or catch Error, but rather catch the specific type of Throwable that you have a workaround for.
Whenever you're catching an Error or Throwable (including Error), you should think carefully about the consequences. Generally for beginners you simply should not do this. But with more experience you may be able to determine if circumstances warrant catching the error. Generally you should consider: what's the worst thing that can happen if you don't catch it? What's the worst that can happen if you do catch it? Often letting the JVM crash is best thing to do, and restart it - either immediately, or after you've analyzed and fixed the problem.
Note also that letting an Error go uncaught does not necessarily stop the JVM, especially if there are multiple threads involved. And sometimes it may not even get printed to the screen, depending on the environment you're using. Try putting throw new Error("test") in your code to see what effect it has, if you're not sure.
As for catching unchecked exceptions, that's much more reasonable. It's true that those often (not always) signal a programmer error that a programmer will have to fix. But for many applications, it's still reasonable to keep running the program. Let's say you're processing a payroll for all employees, and a few of the employees have left their middle name blank (perhaps they don't have a middle name), causing a NullPointerException. Should you not process any employees because some throw exceptions? I think probably you should try to process all of them, and fix problems for the remaining employees as soon as possible. For example: