简体   繁体   English

Java:确切的异常类型有多重要

[英]Java: How important is the exact exception type

How important it is (in programming languages in generally actually) the exact type of exception type? (通常在编程语言中)异常类型的确切类型有多重要? Say I have a hierarchy of chapters of a book, and I use a method to access the chapters. 假设我有一本书的章节层次结构,并且使用一种方法来访问这些章节。 One would be: 一种是:

public void chapterSearch(int chapter) {
    try {
    chapterString = this.hierarchy.get(chapter).getText();
    } catch(IndexOutOfBoundsException e) {
    throw new....
        }
}

The second is this: 第二个是这样的:

public void chapterSearch(String chapterString) {
    for(int i = 0; i < this.hierarchy.size(); i++) {
    if(chapterString.equals(this.hierarchy.get(i).getText())) {
        break;
    } else if(i == this.hierarchy.size() - 1) {
        throw new IllegalArgumentException...
        }
    }
}

In the first method, it is obvious throw a new IndexOutOfBoundsException . 在第一种方法中,很明显抛出新的IndexOutOfBoundsException But then there is the IllegalArgumentException which you can use, which according to the javadoc is 但是然后有一个IllegalArgumentException可以使用,根据javadoc的说法是

Thrown to indicate that a method has been passed an illegal or inappropriate argument. 被抛出以指示方法已传递了非法或不适当的参数。

which seems quite appropriate for the situation, too. 这似乎也很适合这种情况。 I mean, passing a very large chapter number where there is no such chapter number seems quite inappropriate to me. 我的意思是,在没有这样的章节号的情况下传递非常大的章节号对我来说似乎是不合适的 Or, as in the comments, I can throw my own new Exception, ChapterNotFoundException . 或者,如注释中所述,我可以抛出自己的新Exception ChapterNotFoundException But this might be cumbersome, and I might not be using this Exception more than once -- it might then seem that using a pre-existing Exception is more apt. 但这可能很麻烦,并且我可能不多次使用此Exception -似乎使用预先存在的Exception更为合适。

But in general, even if in this case IndexOutOfBoundsException remains most appropriate, or if you would prefer, ChapterNotFoundException , how much does the choice of exception type matter in general? 但是总的来说,即使在这种情况下IndexOutOfBoundsException仍然是最合适的,或者如果您希望使用ChapterNotFoundException ,那么对异常类型的选择总体上ChapterNotFoundException多重要呢?

I guess creating your own Exception type makes loads of sense once you use it a lot, but what if you aren't? 我猜想,一旦使用很多,创建自己的Exception类型就很有意义,但是如果不使用该类型呢?

Edit: A good, related answer, though the answer below is very good as well, is here . 编辑:一个很好的相关答案,尽管下面的答案也很好,但是在这里

I think it depends on how you want to handle the exception. 我认为这取决于您要如何处理异常。 If you want the program to fail then later dig into the log, I would vote for 如果您希望程序失败,请稍后再查看日志,我投赞成票

throw new IllegalArgumentException("Unexpected chapter: \"" + chapterString + "\"");

If you want to catch the exception and deal with it, then creating a ChapterNotFoundException makes more sense. 如果要捕获异常并对其进行处理,则创建ChapterNotFoundException更为有意义。 However, if you want to deal with it in your code, you should think about creating a boolean method that checks if the chapter exists, and avoid handling the exception at all. 但是,如果要在代码中处理它,则应考虑创建一个布尔方法来检查该章是否存在,并且根本避免处理异常。

By Good coding guidelines it is very important to capture the exact exceptions (as far as possible) . 根据良好的编码准则,捕获准确的异常(尽可能)是非常重要的。

Reasons as below 原因如下

  1. There are situation when you need to handle exception and do some repair work in case of exceptions. 在某些情况下,您需要处理异常并在发生异常的情况下进行一些修复工作。 The repair work that you do could be different in case of different exceptions and there can't be a generic way of handling it. 在例外情况不同的情况下,您所做的维修工作可能会有所不同,并且没有通用的处理方式。 So in these situation you can handle specific cases in appropriate catch blocks of the exceptions. 因此,在这种情况下,您可以在适当的异常捕获块中处理特定情况。
  2. Second reason being, if some body is trying to invoke your method, if they know a set of exceptions that could possible be thrown then it's helpful for 3rd part who is going to re-use your method. 第二个原因是,如果某人正在尝试调用您的方法,则如果他们知道可能会抛出的一组异常,那么这对于要重用您的方法的第三部分很有帮助。 If you simply throw "Exception" or "Throwable" , then it becomes too generic under the sky to handle it. 如果仅抛出“ Exception”或“ Throwable”,那么它在天空下变得太普通而无法处理。

To say it in a line, it's always better to answer to the point than beating around the bush 一言一行,回答问题总是比在丛林中跳动更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM