我怎么能以一种安全可读的方式处理我所知道的永远不会被抛出的IOException? [英] How can I handle an IOException which I know can never be thrown, in a safe and readable manner?

查看:232
本文介绍了我怎么能以一种安全可读的方式处理我所知道的永远不会被抛出的IOException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能出错的东西与
不可能出错的东西之间的主要区别是当
a不可能出错的时候
出错,通常情况下可能是
无法进入或修复。
-Douglas Adams

"The major difference between a thing that might go wrong and a thing that cannot possibly go wrong is that when a thing that cannot possibly go wrong goes wrong it usually turns out to be impossible to get at or repair." -Douglas Adams

我有一个类FileItems。 FileItems构造函数接受一个文件,如果文件不存在,则抛出异常(FileNotFoundException)。该类的其他方法也涉及文件操作,因此具有抛出FileNotFoundException的能力。我想找一个更好的解决方案。一个解决方案,不需要其他程序员处理所有这些极不可能的FileNotFoundExceptions。

I have an class FileItems. FileItems constructor takes a file, and throws an exception (FileNotFoundException) if the file doesn't exist. Other methods of that class also involve file operations and thus have the ability throw the FileNotFoundException. I would like to find a better solution. A solution which doesn't require that other programmers handle all of these extremely unlikely FileNotFoundExceptions.

事实的事实:


  1. 该文件已被检查存在,但是极不可能的可能性是,通过实际的一些重大错误,该文件可能在此方法调用之前被删除。

  2. 由于1发生的概率非常不同,无法恢复,所以我更愿意定义一个未经检查的异常。

  3. 该文件已被发现存在,强制其他程序员写代码和抓取检查的FileNotFoundException似乎乏味无用。这个程序应该完全失败。例如,总是有机会计算机可能着火,但没有人会疯狂地迫使其他程序员把它当作一个检查的例外

  4. 我经常遇到这种异常问题,并在每次遇到这个问题时定义自定义未检查的异常(我的旧解决方案)令人厌烦,并增加了代码膨胀。

  1. The file has been checked to exist but the extremely unlikely possibility exists that through some major fault of reality the file might be deleted before this method is called.
  2. Since the probability of 1 happening is extremely unlike and unrecoverable, I would prefer to define an unchecked exception.
  3. The file is has already been found to exist, forcing other programmers to write code and catch the checked FileNotFoundException seems tedious and useless. The program should just fail completely at that point. For example there is always the chance that a computer may catch fire, but no one is insane enough to force other programmers to handle that as a checked exception.
  4. I run into this sort of Exception issue from time to time, and defining custom unchecked exceptions each time I encounter this problem (my old solution) is tiresome and adds to code-bloat.

代码目前看起来像这样

 public Iterator getFileItemsIterator() {
    try{
        Scanner sc = new Scanner(this.fileWhichIsKnowToExist);
        return new specialFileItemsIterator(sc);        
       } catch (FileNotFoundException e){ //can never happen} 

    return null;
 }

如果没有定义自定义的未选中的FileNotFoundException,我该如何做得更好?有没有办法将checkedException转换为uncheckException?

How can I do this better, without defining a custom unchecked FileNotFoundException? Is there some way to cast a checkedException to an uncheckException?

推荐答案

通常的处理方式是异常链接。您只需在RuntimeException中包装FileNotFoundException:

The usual pattern to deal with this is exception chaining. You just wrap the FileNotFoundException in a RuntimeException:

catch(FileNotFoundException e) {
    throw new RuntimeException(e);
}

此模式不仅适用于特定情况下不能发生异常(例如你的),而且当你没有意图或意图真正处理异常(例如数据库链接失败)。

This pattern is not only applicable when an Exception cannot occur in the specific situation (such as yours), but also when you have no means or intention to really handle the exception (such as a database link failure).

编辑:请注意这个类似的反模式,我在野外看到的太多了:

Edit: Beware of this similar-looking anti-pattern, which I have seen in the wild far too often:

catch(FileNotFoundException e) {
    throw new RuntimeException(e.getMessage());
}

通过这样做,您将丢弃原始堆栈跟踪中的所有重要信息,

By doing this, you throw away all the important information in the original stacktrace, which will often make problems difficult to track down.

另一个编辑:正如ThorbjørnRavn Andersen在回应中正确指出的那样,伤害说明你为什么链接异常,无论是在评论中还是甚至更好的,作为例外消息:

Another edit: As Thorbjørn Ravn Andersen correctly points out in his response, it doesn't hurt to state why you're chaining the exception, either in a comment or, even better, as the exception message:

catch(FileNotFoundException e) {
    throw new RuntimeException(
        "This should never happen, I know this file exists", e);
}

这篇关于我怎么能以一种安全可读的方式处理我所知道的永远不会被抛出的IOException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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