简体   繁体   中英

Isn't an unchecked exception that is caught in a try block a checked exception in Java?

有人告诉我,在Java中,未经检查的异常可以在try块中捕获,但如果它被捕获,是不是称为已检查异常?

Unchecked exceptions are exceptions that don't need to be caught in a try - catch block. Unchecked exceptions are subclasses of the RuntimeException or Error classes.

Checked exceptions are exceptions that need to be caught in a try - catch block.

The definition of checked and unchecked exceptions can be found in Section 11.2: Compile-Time Checking of Exceptions of The Java Language Specification :

The unchecked exceptions classes are the class RuntimeException and its subclasses, and the class Error and its subclasses. All other exception classes are checked exception classes.

Just because an unchecked exception is caught in a catch block does not make it a checked exception -- it just means that the unchecked exception was caught, and was handled in the catch block.

One could catch an unchecked exception, and then throw a new checked exception, so any methods calling that method where an unchecked exception could occur and force an method that calls it to handle some kind of exception.

For example, a NumberFormatException which can be thrown when handling some unparsable String to the Integer.parseInt method is an unchecked exception, so it does not need to be caught. However, the method calling that method may want its caller to properly handle such a problem, so it may throw another exception which is checked (not a subclass of RuntimeException .):

public int getIntegerFromInput(String s) throws BadInputException {
    int i = 0;
    try {
        i = Integer.parseInt(s);
    catch (NumberFormatException e) {
        throw new BadInputException();
    }

    return i;
}

In the above example, a NumberFormatException is caught in the try - catch block, and a new BadInputException (which is intended to be a checked exception) is thrown.

Any caller to the getIntegerFromInput method will be forced to catch a BadInputException , and forced to deal with bad inputs. If the NumberFormatException were not to be caught and handled, any callers to this method would have to handle the exception correctly.

(Also, it should be noted, eating an exception and doing something that is not really meaningful is not considered a good practice -- handle exceptions where meaningful exception handling can be performed.)

From The Java Tutorials:

No, it's not called a checked exception just because it is caught. A catch block can be written to catch any kind of Exception or Error. Checked exceptions are those that are subject to the Catch or Specify Requirement , meaning that you are required to either catch them or declare that your method may throw them. You can think of the term checked as meaning that the compiler will check to make sure you adhere to the catch or specify requirement. Errors and RuntimeExceptions are called unchecked exceptions because the compiler does not enforce this requirement on them.

I think the distinction is that the compiler will flag uncaught checked exceptions and methods that throw checked exceptions but don't declare them in the method signature at compile time.

Unchecked exceptions don't require declaration or catching, but neither are prohibited. The compiler just doesn't identify them as errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM