何时应该检查异常/未检查异常? [英] When should checked exception/unchecked exception be chosen?

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

问题描述

我从各种教程中学到:如果一个客户端可以合理地期望从一个异常恢复,使它成为一个检查异常。如果客户端不能做任何事情从异常恢复,使它成为一个未经检查的异常。



我真的想通过一些代码示例看到前面语句的有效性。
例如

  try {
br.readLine();
} catch(IOException e){

e.printStackTrace();
}

这里,IOException被检查Exception.So,我应该如何恢复当这种异常发生?在这里,我排除异常日志记录,异常重新抛出任务,因为他们实际上没有恢复,即做正确的事情。那么,在这里应该应用什么修改来从中恢复?



如果有一种方法来从它恢复,那么同样的方法可以应用于以下代码:

 尝试{
Integer.parseInt(ghg4);
} catch(NumberFormatException nfe){
}



此处NumberFormatException是一个运行时/ unchecked exception.So如果有一种方法可以从它恢复,那么为什么它首先声明为运行时异常?

解决方案

p>我看到三种类型的异常。在一个极端是你不能做任何事情,如一个NullPointerException。你将在代码中的非常高的级别或根本不处理这个。这是可笑的检查它。



另一端是提供有意义的信息。它们是一种返回值的方式,有时是一个复杂的方法,当方法已经有一个返回值。他们也是一个简单的方法来跳跃调用堆栈。 (我可以写一本关于这个的书,但我会停在这里。)EOFException 应该是一个很好的例子。文件有它们的结束,你会迟早击中它,你不想每次你做一个阅读时检查它。在这种情况下,将调用已检查的异常。 (我认为user1291492会同意我的意见。)这可能发生,任何人调用read方法应该为它准备。



现在,使用此类型的异常,您不要要放入堆栈跟踪!它花费大量的时间。调用者只需要知道他打了一个EOF,而不是在IO系统的深处发生了!此外,除非有有趣的信息被返回,异常本身应该是一个 final static 引用,生成一次并用于发生的每个EOF。



中间的第三种类型是Java库使用的类型,例如 real EOFException。他们没有意义。调用者期望永远不会得到一个EOF(例如,他把自己的标记出来)和EOFException是一个与NullPointerException相同的性质,他期望它,不需要麻烦并且丢失堆栈跟踪的处理时间。我认为问题是Java设计人员自己 - 我不得不承认,当我想到这个问题时,这是很少 - 不知道哪些前两个类别这些异常可能会落入。即使在同一个程序中,在一个地方EOFException可能指示程序的总失败。在另一个,它可能是正常的方式来发现一个文件已被读取。所以最终的结果是一大堆异常,做这两个工作,做得很差,迫使程序员使用 try catch

$ <$ c $> <$ c $> p> 添加:我应该明确指出,你可以从真正的EOFException中恢复,只需接受你已经读完文件并继续执行,可能会有一个 break catch 块中的code>语句。然而,还有其他正确的方法来捕获EOF,所以EOFException通常意味着一个真正的问题,如NullPointerException。奇怪的是,我使用NumberFormatException的方式,我认为应该被检查。当我想要一个数字时获得AAA很常见,用户错误,而不是编程错误。


I have learned from various tutorial that "If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception."

I really want to see the effectiveness of previous statement through some code examples. e.g.

try {
        br.readLine();
    } catch (IOException e) {

        e.printStackTrace();
    }

Here, IOException is checked Exception.So, how I'm supposed to recover when this Exception occurs? Here, I'm excluding the exception logging,exception re-throwing tasks since they are not actually recovering i.e making things right. So, what modification should be applied here to recover from it?

If there's a way to recover from it, then the same kind of approach can be applied to the following code :

 try{
    Integer.parseInt("ghg4");
 }catch(NumberFormatException nfe){   
   }

Here NumberFormatException is a Runtime/unchecked exception.So if there's a way to recover from it, then why is it declared as Runtime exception in the first place?

解决方案

I see three types of exceptions. At the one extreme are the ones you can't do anything about, like a NullPointerException. You're going to handle this at a very high level in your code or not at all. It would be ridiculous to check it.

At the other end are the ones that provide meaningful information. They're sort of a way of returning a value, sometimes a complex one, when the method already has a return value. They're also an easy way of jumping up the call stack. (I could write a book about this, but I'll stop here.) An EOFException ought to be a good example of this. Files have their ends, you're going to hit it sooner or later, and you don't want to have to check it every time you do a read. In this case, a checked exception is called for. (I think user1291492 would agree with me on this.) It can happen, and anybody calling a read method should be prepared for it. They'll much prefer a compiler error to a runtime error.

Now, with this type of exception, you do not want to put in a stack trace!!! It costs lots of time. The caller just needs to know he hit an EOF, not where in the depths of the IO system it happened! Further, unless there is interesting information to be returned, the exception itself ought to be a final static reference, generated once and used for every EOF that happens.

The third type, in the middle, are the ones the Java libraries use, like the real EOFException. They make no sense. Either the caller expects never to get an EOF (he put his own marker out there, for instance) and EOFException is of the same nature as a NullPointerException, or he expects it and doesn't need the hassle and lost processing time of a stack trace. I think the problem is that the Java designers themselves--and I have to admit to having this problem myself when I think about it--which is rarely--weren't sure which of the first two categories these exceptions might fall into. Even in the same program, in one place the EOFException might indicate the total failure of a program. In another, it might be the normal way to find out that a file has been read. So the end result is a ton of exceptions that do both jobs and do them poorly, forcing the programmer to use try and catch and throws when they can't do anything anyway and handing them elaborate stack traces they don't need.

Addition: I should pehaps note explicitly that you can recover from the real EOFException by simply accepting that you have finished reading the file and carrying on, possibly with a break statement in the catch block. However, there are other, proper ways to catch an EOF, so an EOFException usually means a real problem like a NullPointerException. Oddly, the way I use NumberFormatException, I think it should be checked. Getting "AAA" when I want a number is very common, a user error, not a programming error.

这篇关于何时应该检查异常/未检查异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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