未处理的异常类型Exception [英] Unhandled exception type Exception

查看:54
本文介绍了未处理的异常类型Exception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简化的类,描述了我的问题:

This is a simplified class that describes my problem:

public class Main {

    enum Test{
        First(method()){ // Unhandled exception type Exception
            // ...
        };

        Test(Object obj){
            //...
        }
    }

    static Object method() throws Exception{
        // ...
        if (someCondition){
            throw new Exception();
        }

    }
}

someCondition 以上取决于设备和某些情况,我现在无法决定,也如您所见,我不想在<代码>方法.

Above someCondition depends on device and some situations and I can not decide in about it now, also as you can see, I do not want to catch Exception in method.

推荐答案

是.这是编译错误.

不.没有特殊的语法可以解决这个问题.

No. There is no special syntax to deal with this.

我不想在方法中捕获异常.

I do not want to catch Exception in method.

不幸的是,如果引发了检查异常,则必须在调用堆栈的更深处捕获 .这是Java语言的基本设计原则,也是编译器严格执行的原则.

Unfortunately if you throw a checked exception, it has to be caught further up the call stack. That is a fundamental design principal for the Java language, and one that the compiler enforces strictly.

在这种情况下,无法捕获已检查的异常.因此,如果要使用枚举常量参数(根据您的代码)调用方法,则该方法无法引发已检查的异常 1 .

In this, case there is no way to catch the checked exception. Hence, if you are going to call a method in enum constant parameter (as per your code), the method cannot throw a checked exception1.

这是一个可能的解决方法,尽管这可能不是一个好主意:

Here is a possible workaround, though this is probably a bad idea:

public class Main {

    enum Test{
        First(methodCatchingException()){
            // ...
        };

        Test(Object obj){
            //...
        }
    }

    static Object method() throws Exception{
        // ...
        if (someCondition){
            throw new Exception();
        }
    }

    static Object methodCatchingException() {
        try {
            return method();
        } catch (Exception ex) {
            throw new SomeRuntimeException("the sky is falling!", ex);
        }
    }
}

解决此问题的另一种方法是问自己,如果编译器允许您编写该内容并引发了异常,则该异常会发生什么情况?它会去哪里?

Another way to look at this problem is to ask yourself what should happen with the exception if the compiler let you write that ... and an exception was thrown? Where would it go?

  • 您无法抓住它...因为 enum 初始化类似于 static 初始化.
  • 如果Java运行时完全忽略抛出的异常,那将是非常糟糕的事情.
  • 如果Java运行时崩溃,则检查的异常模型将被破坏.

所以,这对我说的是Java语言设计是正确的,Java编译器是正确的……而这里的真正问题出在您的应用程序 design 中:

So, what this is saying to me is that the Java language design is right, the Java compiler is right ... and the real problem here is in your application design:

  • 您不应在此处传播已检查的异常.如果在这种情况下发生异常,则它绝对不是可恢复的错误.

  • You should not be propagating a checked exception here. If an exception occurs in this context it is categorically NOT a recoverable error.

为此可能不建议使用枚举...,因为可能会出现不可恢复的初始化错误.

Maybe it is inadvisable to use an enum for this ... because of the potential for non-recoverable initialization errors.

(请注意,如果此方法调用由于未检查的异常而终止,它将变成 ExceptionInInitializerError .此外,JVM会将 enum 类标记为无法初始化,并且如果您的应用程序尝试使用它,则会抛出一个 NoClassDefFoundError ;例如,通过 Class.forName(...).)

(Note that if this method call terminates due to an unchecked exception, it will turn it into an ExceptionInInitializerError. In addition, the JVM will mark the enum class as uninitializable, and will throw an NoClassDefFoundError if your application attempts to use it; e.g. via Class.forName(...).)

我假设此处将 Exception 用于说明目的.将方法声明为引发异常引发新的Exception(...)

I assume that Exception is used here for illustration purposes. It is a bad thing to declare methods as throws Exception or to throw new Exception(...)

1-我看了一下JLS,以了解一些支持它的内容.据我所知,规范没有提到这种情况.我希望看到它在 JLS 11.2.3 .但是,很明显,编译器无法在该点传播受检查的异常,因为它会破坏"受检查的异常的工作模式.

1 - I had a look at the JLS for something to back this up. As far as I can tell, the spec does not mention this situation. I'd have expected to see it listed in JLS 11.2.3. However, it is clear that a compiler cannot allow a checked exception to propagate at that point as it would "break" the model of how checked exceptions work.

这篇关于未处理的异常类型Exception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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