问题描述:

Example bellow a program ,where in try block defectedCode() method is called ,So why only output shown only C with "Exception in thread "main" java.lang.Error".

public class ExceptionTest {

public static void defectedCode(){

throw new Error();

}

public static void main(String args[]){

try{

defectedCode();

System.out.println("A");

}catch(Exception e){

System.out.println("B");

}finally{

System.out.println("C");

}

System.out.print("D");

}

}

Exception in thread "main" java.lang.Error

C

at ExceptionTest.defectedCode(ExceptionTest.java:15)

at ExceptionTest.main(ExceptionTest.java:21)

Java Result: 1

网友答案:

The main reason for this is that you throw an Error but you catch an Exception. If you look at the Throwable hierarchy the point is clear. You can't catch an Error with an Exception. Hence the catch block isn't entered and finally will be called.

Try this:

    try{
        defectedCode();
        System.out.println("A");

    }catch(Throwable e){

        System.out.println("B");

    }finally{

        System.out.println("C");
    }

网友答案:

Because Error is not Exception, so the catch block is not monitoring it

use throw new Exception()

网友答案:

An Error is not an Exception.

The base class for throwable objects is Throwable.
Errors and exceptions are two different types of throwables. However, errors are usually not supposed to be caught, which is why people use catch(Exception e) to catch basically all exceptions that they should catch.

Obviously, since Error is not a subclass of Exception, it's not affected by catch(Exception e), so it's not caught. finally is always executed, regardless of whether the throwable has been caught or not.

网友答案:

You're not supposed to catch errors

An Error "indicates serious problems that a reasonable application should not try to catch."

while

An Exception "indicates conditions that a reasonable application might want to catch."

Talking about your code, you're throwing an error and catching an exception, it must be evident by now that they are 2 discrete entities

Error along with RuntimeException & their subclasses are unchecked exceptions. All other Exception classes are checked exceptions.

Checked exceptions are generally those from which a program can recover & it might be a good idea to recover from such exceptions programmatically. Examples include FileNotFoundException, ParseException, etc. A programmer is expected to check for these exceptions by using the try-catch block or throw it back to the caller

On the other hand we have unchecked exceptions. These are those exceptions that might not happen if everything is in order, but they do occur. Examples include ArrayIndexOutOfBoundException, ClassCastException, etc. Many applications will use try-catch or throws clause for RuntimeExceptions & their subclasses but from the language perspective it is not required to do so. Do note that recovery from a RuntimeException is generally possible but the guys who designed the class/exception deemed it unnecessary for the end programmer to check for such exceptions.

Errors are also unchecked exception & the programmer is not required to do anything with these. In fact it is a bad idea to use a try-catch clause for Errors. Most often, recovery from an Error is not possible & the program should be allowed to terminate. Examples include OutOfMemoryError, StackOverflowError, etc.

Do note that although Errors are unchecked exceptions, we shouldn't try to deal with them, but it is ok to deal with RuntimeExceptions(also unchecked exceptions) in code. Checked exceptions should be handled by the code.

网友答案:

Though ethically you should not catch an error, you can still catch that as a Throwable Object.

相关阅读:
Top