1

I want to know whether an error can be handled or not in Java in the similar way to exceptions. I have seen errors that can't be handled such as AssertionError and I've also tried throwing an error inside a program and tried handling using catch, It worked and also the next part of code did execute(This is contradictory to the part where the program supposed to be exited whenever an error is encountered). Is it like few errors can be handled and few others cannot be. I'm confused with this, can anyone clear this doubt of mine and state the difference between errors and exceptions?

package package1;

public class Class1 
{   
public static void main(String[] args)
{
    Class1 cl1=new Class1();
    int x=2;
    String s = null;
    //assert(x<0):"x is not less than zero";
    try
    {
        cl1.workonExceptions();
    }
    catch (Error e)     
    {
        e.printStackTrace();
    }
    System.out.println("Not Terminated yet");
}
public void workonExceptions() throws Error 
{                                               
    try                                         
    {
        throw new Exception("Exception");
    }
    catch (Exception e) 
    {
        throw new Error("Exception Again");         }
}
}
4

3 回答 3

4

Some background on Error: It's not intended to be catchable; it only comes up when something goes really wrong. An exception is meant to be caught and handled, since it's something that may come up on occasion, but shouldn't interrupt or interfere severely with the running of your program, such as dividing by zero.

As for it working inside of a try...catch statement, the reason is that this is a property of the Throwable class.

Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement. Similarly, only this class or one of its subclasses can be the argument type in a catch clause.

于 2012-06-08T22:57:36.070 回答
1

Exceptions, if thrown from a method, are required by the compiler to be caught by callers of that method.

Errors, even if thrown from a method, are not required by the compiler to be caught by callers of the method.

Both Exceptions and Errors CAN be caught by callers, and behave the same way when they are. If you have code that illustrates "continues to execute", I'd be interested in seeing it. If you throw either of these (both derived from Throwable), execution of the method from which they are thrown ceases at that point (absent finally clauses, an (ahem) exception to the rule I'd rather not try to cover in this post).

于 2012-06-08T23:02:41.593 回答
1

All errors in Java are catchable, including AssertionError, although it is not recommended to catch them, and it is not guaranteed that the JVM is in consistent enough state to continue execution after an Error. So what you see is completely normal.

Differently from C#, even ThreadDeath is handled pretty much the same as any other Throwable (that is, it can be caught and consumed).

The main difference between errors and exceptions is that methods can throw any errors regardless of their exception specifiers.

于 2012-06-08T23:05:27.530 回答