You can catch them but it would not be the right thing to do. Basically errors are something which crops up due to an error in the underlying enviornment. Though even if you catch it, I guess it would be quite difficult to correct it. So I would suggest it's better left unhandled.
As mentioned, Errors generally indicate an irrecoverable problem. Often, that means you should not catch them at all.
However, in a multi-threaded program, an Error in a background thread might result in its silent death and your great confusion. In such programs, you might want to install some sort of logging of all Throwables, when they are about to terminate a thread. Make sure that your logging code is very simple, so it can't have problems itself. Generally, rethrow the Throwable after logging it.
In some of our applications, we catch OutOfMemoryError. It is a bit different to other Errors. One cannot really recover fully from it, as one doesn't know how what state it was in when heap was exhausted, but one can attempt an orderly, logged shut-down.
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
OutOfMemory seems to be one you can handle. If you try to create a 12 meg buffer and get out of memory, try a 4 meg buffer? I've seen them in my HTTP server logs ... one request fails to allocate some huge object but the other threads go on happily enough.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: Oct 30, 2001
OutOfMemoryError can occur at any time, including half-way through a set of steps that normally either fully complete or fully roll back. The state of your Java program after OutOfMemoryError is often undefined and it is often unsafe to do anything beyond attempting an orderly shut-down of your program.
An exception to this would be if you enclose just one huge array allocation in a try..catch. In that situation, I think you can be sure that an OutOfMemoryError has not caused unexpected havoc. And, as you say, this is also a situation where you may genuinely be able to take alternative action, such as reducing the size of array you ask for.
Hello Peter, Certainly what you told makes sense, Is there any way to prevent this OutOfMemoryError, One way to prevent this error,(by specifying the max and min heap size) java -ms64m -mx256m CardsBatchProcess
Is there any other alternative ? or a better way ?