简体   繁体   English

是不是在try块中捕获的未经检查的异常在Java中检查了异常?

[英]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. 未经检查的异常是不需要在try - catch块中捕获的异常。 Unchecked exceptions are subclasses of the RuntimeException or Error classes. 未经检查的异常是RuntimeExceptionError类的子类。

Checked exceptions are exceptions that need to be caught in a try - catch block. 已检查的异常是需要在try - catch块中捕获的异常。

The definition of checked and unchecked exceptions can be found in Section 11.2: Compile-Time Checking of Exceptions of The Java Language Specification : 有关已检查和未检查异常的定义,请参见第11.2节:编译时检查 Java语言规范 的异常

The unchecked exceptions classes are the class RuntimeException and its subclasses, and the class Error and its subclasses. 未经检查的异常类是RuntimeException类及其子类,以及类Error及其子类。 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. 仅仅因为在catch块中捕获未经检查的异常不会使其成为已检查的异常 - 它只是意味着捕获了未经检查的异常,并且在catch块中处理。

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. 可以catch未经检查的异常,然后throw一个新的已检查异常,因此调用该方法的任何方法都可能发生未经检查的异常,并强制调用它来处理某种异常的方法。

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. 例如,在向Integer.parseInt方法处理一些不可解析的String时可以抛出的NumberFormatException是一个未经检查的异常,因此不需要捕获它。 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 .): 但是,调用该方法的方法可能希望其调用者正确处理这样的问题,因此它可能会抛出另一个被检查的异常(不是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. 在上面的示例中,在try - catch块中捕获NumberFormatException ,并BadInputException新的BadInputException (旨在作为已检查的异常)。

Any caller to the getIntegerFromInput method will be forced to catch a BadInputException , and forced to deal with bad inputs. getIntegerFromInput方法的任何调用者都将被强制捕获BadInputException ,并被迫处理错误的输入。 If the NumberFormatException were not to be caught and handled, any callers to this method would have to handle the exception correctly. 如果不捕获和处理NumberFormatException则此方法的任何调用者都必须正确处理异常。

(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: 来自Java教程:

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. 可以编写一个catch块来捕获任何类型的异常或错误。 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. 已检查的异常是受Catch或Specify Requirement约束的异常,这意味着您需要捕获它们或声明您的方法可能抛出它们。 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. 错误和RuntimeExceptions被称为未经检查的异常,因为编译器不对它们强制执行此要求。

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. 编译器只是不将它们识别为错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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