简体   繁体   English

Java:异常处理中的未知错误

[英]Java : Unknown error in exception handling

i have a weird question. 我有一个奇怪的问题。 i had a quiz in my class today. 我今天上课有一个测验。 One portion of the quiz was to find and correct errors in a short piece of code. 测验的一部分是在一小段代码中查找并纠正错误。 one of the questions was like this 问题之一就是这样

 class Example {
    public static void main(String[] args) {
        try {
            System.out.println("xyz");
        } catch (Exception e) {
            System.out.println("Exception caught");
        } finally {
            System.out.println("abc");
        }
    }
 }

I thought there was no error in the program but my professor insisted that there was. 我以为程序没有错误,但我的教授坚持认为有错误。 Can anyone guess what the error is? 谁能猜出错误是什么?

The "error" may be that you don't need to handle any exception here: System.out.println does not specify any checked exception. “错误”可能是您不需要在此处处理任何异常: System.out.println没有指定任何检查的异常。 It could simply be: 它可能只是:

public static void main(String[] args) {        
     System.out.println("xyz");        
}

Since the Exception class covers both checked and unchecked exceptions, then if you add a catch block here, in this case you would be handling only unchecked exceptions, which you should not normally handle. 由于Exception类同时包含已检查和未检查的异常,因此,如果在此处添加catch块,则在这种情况下,您将仅处理未检查的异常,通常不应处理。

There is no Error in the Above Program , but also there is no need to put a try{} catch{} ....since you don't use any code that can throw an Exception , for example a risky method like Thread.sleep(); 上述程序中没有错误,但是也不需要放置try{} catch{} ...。因为您不使用任何可能引发Exception的代码,例如Thread.sleep();类的危险方法Thread.sleep();

So maybe that's what your professor meant . 所以也许这就是你的教授的意思。

Well, I see nothing that would keep this from compiling, but I do see some problems. 好吧,我看不到任何东西可以阻止它的编译,但是我确实看到了一些问题。 To begin with, there are comments indicating the presence of code which is not there. 首先,有注释表明存在不存在的代码。 Comments out of sync with code is always a problem. 注释与代码不同步始终是一个问题。 [EDIT: indentation errors have been edited away] And you're catching Exception e, which you really oughtn't to do. [编辑:缩进错误已被删除]并且您正在捕获异常e,这是您真正不应该做的。 You should always catch a specific exception that you expect to encounter, and handle it specifically. 您应该始终捕获预期会遇到的特定异常,并进行专门处理。 Since there's no exception that System.out.println can throw, this would make the whole Exception handling block a problem. 由于没有System.out.println可以抛出的异常,这将使整个Exception处理块成为问题。

The following code snippet would throw a compilation error if used with IOException , since System.out.println would never throw an IOException but could throw Exception or Throwable which is its super class. 如果将以下代码段与IOException使用,则将引发编译错误,因为System.out.println绝不会抛出IOException而可能抛出ExceptionThrowable ,这是它的超类。

try {
    System.out.println("xyz");
} catch (IOException e) {
    //simple display error statement here
} finally {
    //simple print statement here
}

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

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