Why should not we catch Errors in Java?

10,569

Solution 1

The reason that you shouldnt generally attempt to handle these errors is because more often than not there wont be anything at all you can do about them.

They will tend to be JVM level errors, not application level ones - OutOfMemory is a good example here. If the JVM has run out of memory what would your program do? And even if you did catch it there is no guarentee that the handling code would complete/proceed in a consistent manner, given the terminal condition thrown

Solution 2

You can catch anything that is Throwable which means you can catch Error. But an Error represents a serious problem and not advisable to catch.

From Java API:

"An Error is a subclass of Throwable that indicates serious problems that a reasonable >application should not try to catch. Most such errors are abnormal conditions. The >ThreadDeath error, though a "normal" condition, is also a subclass of Error because most >applications should not try to catch it."

Error represents some critical problem with your application. For example OutOfMemoryError will be thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector. So if by catching OutOfMemoryError will not solve your actual problems except increasing the memory and by catching this Error in program may not lead any solution and rather causes error prone application. So, it is not advisable to catch errors.

Solution 3

The Error types actually we can't guess this will come perticular block of Code like below.

try {
    // not guaranty OutOf memeroy Exception will come from this block      
} catch (OutOfMemoryError ex) {
    // handling code
}

That's why we will not handle.

Solution 4

There is an Error when the JVM is no more working as expected, or is on the verge to. If you catch an error, there is no guarantee that the catch block will run, and even less that it will run till the end.

It will also depend on the running computer, the current memory state, so there is no way to test, try and do your best. You will only have an hasardous result.

You will also downgrade the readability of your code.

Share:
10,569
lies
Author by

lies

Updated on June 06, 2022

Comments

  • lies
    lies almost 2 years

    I know, that it harms application performance, but should not we surround places of code, which really risky with e.g.

    try {
        // code        
    } catch (OutOfMemoryError ex) {
        // handling code
    }
    

    It looks pretty safe.

    From docs:

    An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch.

    Why not?

  • djangofan
    djangofan about 6 years
    I know you said If you catch an error, there is no guarantee that the catch block will run, and even less that it will run till the end. but is there any proof of that statement anywhere? Point me to a link somewhere?
  • Nicolas Zozol
    Nicolas Zozol about 6 years
    docs.oracle.com/javase/specs/jvms/se7/html/jvms-6.html#jvms-‌​6.3 ; This error is delivered asynchronously (§2.10) when it is detected and may occur at any point in a program : if the jvm bugs, it bugs. If electric power shutdowns, the jvm dies. You may not survive until the error is catched.