Joshua Bloch 在“Effective Java”中说过
Use checked exceptions for recoverable conditions and runtime exceptions for programming errors (Item 58 in 2nd edition)
让我们看看我是否理解正确。
这是我对已检查异常的理解:
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 是未经检查的异常吗?
这是我对未经检查的异常的理解:
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.现在,上面的代码不能也是一个检查异常吗?我可以尝试恢复这种情况吗?我可以吗?(注意:我的第三个问题在上面的 catch
内)
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{
}
为什么他们让异常冒泡?越早处理错误不是更好吗?为什么要冒泡?
6.我应该冒泡确切的异常还是使用异常来掩盖它?
以下是我的读数
In Java, when should I create a checked exception, and when should it be a runtime exception?
最佳答案
许多人说根本不应该使用已检查的异常(即您应该明确捕获或重新抛出的异常)。例如,它们在 C# 中被淘汰了,而大多数语言都没有它们。所以你总是可以抛出 RuntimeException
的子类(未经检查的异常)
但是,我认为检查异常很有用 - 当您希望强制 API 的用户思考如何处理异常情况(如果它是可恢复的)时,可以使用它们。只是检查异常在Java平台中被过度使用,让人讨厌。
Here's my extended view on the topic .
至于具体问题:
是否将
NumberFormatException
视为已检查异常?
否。未选中NumberFormatException
(= 是RuntimeException
的子类)。为什么?我不知道。 (但应该有一个方法isValidInteger(..)
)RuntimeException
是未经检查的异常吗?
是的,没错。我应该在这里做什么?
这取决于此代码在哪里以及您希望发生什么。如果它在 UI 层 - 捕获它并显示警告;如果它在服务层中 - 根本不要捕获它 - 让它冒泡。只是不要吞下异常(exception)。如果在大多数情况下发生异常,您应该选择以下之一:- 记录并返回
- 重新抛出(声明由方法抛出)
- 通过在构造函数中传递当前异常来构造一个新异常
现在,上面的代码不能也是检查异常吗?我可以尝试恢复这种情况吗?可以吗?
本来可以的。但也没有什么能阻止你捕捉未经检查的异常为什么人们在 throws 子句中添加类
Exception
?
最常见的原因是人们懒于考虑捕捉什么以及重新抛出什么。抛出Exception
是一种不好的做法,应该避免。
唉,没有单一的规则可以让您确定何时捕获、何时重新抛出、何时使用已检查异常以及何时使用未检查异常。我同意这会导致很多困惑和很多糟糕的代码。 Bloch 陈述了一般原则(您引用了其中的一部分)。并且总的原则是在可以处理的层重新抛出异常。
关于java - 了解 Java 中的已检查与未检查异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6115896/