Java无法访问的catch块编译器错误 [英] Java unreachable catch block compiler error

查看:340
本文介绍了Java无法访问的catch块编译器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在Java中我们可以捕获 Exception ,即使它没有被抛出,但是我们无法捕获它的子类(unchecked RuntimeException s及其子类)。示例代码:

Why in Java can we catch an Exception even if it is not thrown, but we can't catch it's subclass (except for "unchecked" RuntimeExceptions and it subclasses). Example code:

class Test {
    public static void main(String[] args) {
        try {
            // do nothing
        } catch (Exception e) {
            // OK           
        }

        try {
            // do nothing
        } catch (IOException e) {
               // COMPILER ERROR: Unreachable catch block for IOException.
               //This exception is never thrown from the try statement body
        }       
    }
}

任何想法?

推荐答案

A RuntimeException 任何代码都可能抛出$ c>。换句话说,编译器无法轻易预测可以抛出哪种代码。 RuntimeException 可以被 catch(例外e)块捕获。

A RuntimeException could be thrown by any code. In other words, the compiler can't easily predict what kind of code can throw it. A RuntimeException can be caught by a catch(Exception e) block.

IOException 是一个经过检查的异常 - 只有声明抛出它的方法调用才能这样做。编译器可以(合理地)确信它不可能发生,除非有声明抛出它的方法调用。

IOException, however, is a checked exception - only method calls which are declared to throw it can do so. The compiler can be (reasonably) confident that it can't possible occur unless there are method calls which are declared to throw it.

Java编译器根本不考虑在try块中根本没有代码的情况 - 它总是允许你捕获未经检查的异常,因为在所有合理的场景中都会有可能可能抛出的代码未经检查的例外情况。

The Java compiler simply doesn't consider the "there's no code at all within the try block" situation - it always allows you to catch unchecked exceptions, as in all reasonable scenarios there will be code which could potentially throw an unchecked exception.

来自第14.21节


如果以下两种情况都可以访问一个catch块C是的:

A catch block C is reachable iff both of the following are true:


  • try块中的某些表达式或throw语句是可以访问的,并且可以抛出一个异常,其类型可分配给参数catch子句C.(如果包含它的最内层语句可以访问,则表达式被认为是可达的。)

  • try语句中没有先前的catch块,因此C的参数类型与A参数类型的子类相同。

可以说编译器应该意识到在第一种情况下try块中有 no 表达式......看起来这仍然是一个无法访问的条款对我来说。

Arguably the compiler should realize that there are no expressions within the try block in your first case... it looks like this is still an unreachable catch clause, to me.

编辑:如评论中所述,第14.20节包含:

As noted in comments, section 14.20 contains this:


这是一个编译时错误,如果 catch 子句捕获已检查的异常类型 E1 ,但不存在已检查的异常类型 E2 ,以下全部保留:

It is a compile-time error if a catch clause catches checked exception type E1 but there exists no checked exception type E2 such that all of the following hold:


  • E2 < ;: E1

  • 对应 catch 子句的 try 块可以抛出 E2

  • 没有在 catch 之前,立即封闭的try语句的块捕获 E2 E2 的超类型。

  • E2 <: E1
  • The try block corresponding to the catch clause can throw E2
  • No preceding catch block of the immediately enclosing try statement catches E2 or a supertype of E2.

除非 E1 是类异常。

所以看起来这就是你实际上运行犯规的情况,但是规范并不像14.21中无法到达的陷阱那样清晰。

So it looks like that's what you're actually running foul of, but the spec isn't as clear as it could be in terms of unreachable catch blocks in 14.21.

这篇关于Java无法访问的catch块编译器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆