我应该故意捕获检查异常吗?
我设计了一个上传文件处理如下:
UploadFileHandler 是提供检查方法的主类。
public class UploadedFileHandler {
public static void handleUploadedFile(String fileName) {
try {
checkFile(fileName);
} catch (BadUploadedFileException ex) {
deleteFile(fileName);
}
}
private static void checkFile(String fileName) {
new UploadedFileChecker(fileName).check();
}
private static void deleteFile(String fileName) {
//...code to delete the file.
}
}
UploadedFileChecker 进行检查。
public class UploadedFileChecker {
private String fileName;
public UploadedFileChecker(String fileName) {
this.fileName = fileName;
}
public void check() throws BadUploadedFileException {
checkFileFormat();
scanVirus();
}
private void checkFileFormat() {
// if file format unsupported
throw new BadUploadedFileException();
}
private void scanVirus() {
// if contains virus
throw new BadUploadedFileException();
}
}
BadUploadedFileException 声明如下:
public class BadUploadedFileException extends RuntimException {
}
我让它扩展 RuntimeException,因为它使 UploadedFileChecker 中的代码变得干净,但这样做会使其成为未经检查的异常。因此,handleUploadedFile 中的 catch 是不合适的,因为我们不应该捕获未经检查的异常。
我的问题是,我应该捕获 BadUploadedFileException 还是使其扩展 Exception 并将“抛出 BadUploadedFileException”附加到 UploadedFileChecker 的每个方法。
I have designed a uploaded file handling as follow:
The UploadFileHandler is the main class providing checking methods.
public class UploadedFileHandler {
public static void handleUploadedFile(String fileName) {
try {
checkFile(fileName);
} catch (BadUploadedFileException ex) {
deleteFile(fileName);
}
}
private static void checkFile(String fileName) {
new UploadedFileChecker(fileName).check();
}
private static void deleteFile(String fileName) {
//...code to delete the file.
}
}
And the UploadedFileChecker do the checking.
public class UploadedFileChecker {
private String fileName;
public UploadedFileChecker(String fileName) {
this.fileName = fileName;
}
public void check() throws BadUploadedFileException {
checkFileFormat();
scanVirus();
}
private void checkFileFormat() {
// if file format unsupported
throw new BadUploadedFileException();
}
private void scanVirus() {
// if contains virus
throw new BadUploadedFileException();
}
}
And the BadUploadedFileException is declared as follow:
public class BadUploadedFileException extends RuntimException {
}
I let it extend RuntimeException because it makes the code in UploadedFileChecker clean, but doing this make it an unchecked exception. Thus, the catch in handleUploadedFile is inapproriate since we should not catch unchecked exceptions.
My question is, should I catch the BadUploadedFileException or make it extends Exception and append "throws BadUploadedFileException" to every method of UploadedFileChecker.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为你最好让
check()
返回一个布尔值。我相信这会让代码看起来更干净,就像 Brian 所说的那样——异常并不是真正用于逻辑的——它们是在出现意外错误时出现的。
I think you'd be better off making
check()
return a boolean.I believe that'll make the code look cleaner, and like Brian said- exceptions aren't really for logic- they're for when something goes unexpectedly wrong.
如果你遇到例外,你会采取什么措施吗?如果没有,就让它冒泡。如果是,那么您当然可以捕获未检查异常。
are you going to do anything if you get an exception? If not, let it bubble. If you are then you can certainly catch an uncheck exception.
就我个人而言,我认为应该是受检查的异常。你应该概率。从每个文件处理方法中抛出它并在 UploadedFileHandler 中捕获它,适当地处理它,如果您需要不同的处理逻辑,则将 BadUploadedFileException 拆分为许多不同类型的异常。
有关检查和非检查异常的更多信息请参见此处。
Personally, I think it should be a checked exception. You should prob. throw it from each file handling method and catch it in UploadedFileHandler, handling it appropriately, if you need different handling logic then split BadUploadedFileException into a number of different types of exceptions.
More information on checked and unchecked exceptions here.
最后一个选择会更好。我不认为这会使代码更难阅读,至少您(以及即将推出的版本中的同事)会记住处理它。
除此之外,当然不禁止捕获从 RuntimeException 派生的异常。另一方面,捕获 RuntimeException 或 Exception 只能在 main 方法等中完成(它可能是 OutOfMemoryException!)。
如果您使用显式 RuntimeExceptions,请不要在方法规范中使用“throws”关键字,但如果它们看起来很重要,请将它们包含在 JavaDoc 中。
The last option would be preferable. I don't think it makes the code much harder to read, and at least you (and your colleagues in upcoming versions) will be remembering to handle it.
Besides that, it is certainly not forbidden to catch Exceptions derived from RuntimeException. Catching RuntimeException or Exception on the other hand should only be done in main methods and suchlike (it could be an OutOfMemoryException!).
If you use explicit RuntimeExceptions, don't use a "throws" keyword in the method specification but do include them in the JavaDoc if they seem important.
异常应该用于异常条件。你意想不到的事情会发生。
您不应该将它们用于条件逻辑。
Josh Bloch 在他的书中具体概述了这一点,这本书相当不错,恕我直言,这是一本必备的书:
http: //java.sun.com/docs/books/ effective/
Exceptions should be used for exceptional conditions. Things that you don't expect to happen.
You shouldn't use them for conditional logic.
Josh Bloch outlines that specifically in his book, which is quite good and a must-have IMHO:
http://java.sun.com/docs/books/effective/
没有这样的规则。只要您知道如何处理任何异常,您就可以捕获它。
然而,决不能发现错误!
There is no such rule. You catch any exceptions whenever you know what to do with it.
However, one must never catch Errors!