Java:已检查vs未经检查的异常说明 [英] Java: checked vs unchecked exception explanation

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

问题描述

我已经在StackOverFlow上阅读了有关已检查和未经检查的异常的多个帖子。老实说,我还是不太确定如何正确使用它们。

I have read multiple posts on StackOverFlow about checked vs unchecked exceptions. I'm honestly still not quite sure how to use them properly.

Joshua Bloch在 Effective Java 中表示

Joshua Bloch in "Effective Java" said that


使用
可恢复条件的已检查异常和运行时
异常编程错误
(第2版第58项)

Use checked exceptions for recoverable conditions and runtime exceptions for programming errors (Item 58 in 2nd edition)

让我们看看我是否理解这一点。

Let's see if I understand this correctly.

以下是我对已检查例外的理解:

Here is my understanding of a checked exception:

try{
    String userInput = //read in user input
    Long id = Long.parseLong(userInput);
}catch(NumberFormatException e){
    id = 0; //recover the situation by setting the id to 0
}

1。以上是否考虑了经过检查的例外情况?

2。 RuntimeException是未经检查的异常吗?

以下是我对未经检查的异常的理解:

Here is my understanding of an unchecked exception:

try{
    File file = new File("my/file/path");
    FileInputStream fis = new FileInputStream(file);   
}catch(FileNotFoundException e){

//3. What should I do here?
    //Should I "throw new FileNotFoundException("File not found");"?
    //Should I log?
    //Or should I System.exit(0);?
}

4。现在,上述代码也不能成为检查异常吗?我可以尝试恢复这样的情况吗?我可以吗?(注意:我的第3个问题在上面的 catch 里面)

4. Now, couldnt the above code also be a checked exception? I can try to recover the situation like this? Can I? (Note: my 3rd question is inside the catch above)

try{
    String filePath = //read in from user input file path
    File file = new File(filePath);
    FileInputStream fis = new FileInputStream(file);   
}catch(FileNotFoundException e){
    //Kindly prompt the user an error message
    //Somehow ask the user to re-enter the file path.
}

5。为什么人们会这样做?

public void someMethod throws Exception{

}

为什么他们会让异常冒泡?是不是更快地处理错误?为什么泡起来?

Why do they let the exception bubble up? Isn't handling the error sooner better? Why bubble up?

编辑:我应该冒泡确切的异常还是使用异常屏蔽它?

以下是我的读物

在Java中,何时应该创建一个已检查的异常,何时应该是运行时异常?

何时选择已检查和未经检查的例外

推荐答案

许多人说已检查的例外(即您应明确捕获的或不应该使用rethrow)。例如,它们在C#中被淘汰,大多数语言都没有它们。所以你总是可以抛出一个的子类RuntimeException (未经检查的异常)

Many people say that checked exceptions (i.e. these that you should explicitly catch or rethrow) should not be used at all. They were eliminated in C# for example, and most languages don't have them. So you can always throw a subclass of RuntimeException (unchecked exception)

但是,我认为已检查的异常是有用的 - 当您想强制API的用户思考如何处理异常情况(如果它是可恢复的)时,可以使用它们。只是被检查的异常在Java平台中被过度使用,这让人们讨厌它们。

However, I think checked exceptions are useful - they are used when you want to force the user of your API to think how to handle the exceptional situation (if it is recoverable). It's just that checked exceptions are overused in the Java platform, which makes people hate them.

这是我对该主题的扩展视图

至于具体问题:


  1. NumberFormatException 是否考虑检查过的异常?

    No. NumberFormatException 未选中(=是 RuntimeException 的子类)。为什么?我不知道。 (但应该有一个方法 isValidInteger(..)

  1. Is the NumberFormatException consider a checked exception?
    No. NumberFormatException is unchecked (= is subclass of RuntimeException). Why? I don't know. (but there should have been a method isValidInteger(..))

RuntimeException 未经检查的例外?

是,确实。

Is RuntimeException an unchecked exception?
Yes, exactly.

我该怎么办?

这取决于此代码的位置以及您希望发生的代码。如果它在UI层中 - 捕获它并显示警告;如果它在服务层 - 根本不抓住它 - 让它冒泡。只是不要吞下这个例外。如果在大多数情况下发生异常,您应该选择以下其中一种:

What should I do here?
It depends on where this code is and what you want to happen. If it is in the UI layer - catch it and show a warning; if it's in the service layer - don't catch it at all - let it bubble. Just don't swallow the exception. If an exception occurs in most of the cases you should choose one of these:


  • 记录并返回

  • 重新抛出它(声明它被方法抛出)

  • 通过在构造函数中传递当前的一个来构造一个新的异常

现在,上述代码难道也不是一个经过检查的例外吗?我可以尝试恢复这样的情况吗?可以吗?

它可能已经存在。但是没有什么可以阻止你捕捉未经检查的异常

Now, couldn't the above code also be a checked exception? I can try to recover the situation like this? Can I?
It could've been. But nothing stops you from catching the unchecked exception as well

为什么人们会添加类异常在投掷条款中?

最常见的原因是人们懒得考虑要捕获什么以及要重新抛出什么。抛出异常是一种不好的做法,应该避免。

Why do people add class Exception in the throws clause?
Most often because people are lazy to consider what to catch and what to rethrow. Throwing Exception is a bad practice and should be avoided.

唉,没有一条规则可以让您确定何时捕获,何时重新抛出,何时使用已检查以及何时使用未经检查的异常。我同意这会导致很多混乱和很多错误的代码。 Bloch说明了一般原则(你引用了它的一部分)。一般原则是重新抛出可以处理它的图层的异常。

Alas, there is no single rule to let you determine when to catch, when to rethrow, when to use checked and when to use unchecked exceptions. I agree this causes much confusion and a lot of bad code. The general principle is stated by Bloch (you quoted a part of it). And the general principle is to rethrow an exception to the layer where you can handle it.

这篇关于Java:已检查vs未经检查的异常说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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