自定义异常,有多少才算太多?

huangapple go评论56阅读模式
英文:

Custom Exceptions, how much is too much?

问题

我一直在思考我应该有多少个异常,我看到其他帖子说,拥有多个异常有助于提高代码的可读性,并且在出现错误时更好地理解错误。

现在我有5个异常,检查UserNotFoundException和AccountExpenseNotFoundException基本上都是相同的异常,但是涉及不同的实体。

这是一种不好的做法吗?
我应该创建一个名为EntityNotFound的异常来处理这种情况吗?

英文:

I've been wondering how many exceptions should I have, I've seen other posts saying that it is good to have multiple exceptions it improves readability of your code and better understanding of the error if there is one.

Now I have 5 exceptions, check UserNotFoundException and AccountExpenseNotFoundException basically both exceptions are the same, but referring to different entities.

自定义异常,有多少才算太多?

Is this a bad practice?
Should I make an exception like EntityNotFound to handle this?

答案1

得分: 1

不,这很好。唯一的缺点是你最终会写出这样的代码:

try {
   bookkeeperthingie();
} catch (AccountExpenseAlreadyExistsException |
   AccountExpenseNotFoundException | UsernameOrPasswordInvalidException |
   Blablabla | Morebla e) {
}

为了避免这种情况,确保你的异常遵循一个层次结构。例如,你可以有:

public class BookkeepingException extends Exception {
    public BookkeepingException(String msg) { super(msg); }
    public BookkeepingException(String msg, Throwable cause) { super(msg, cause); }
}

public class AccountExpenseAlreadyExistsException extends BookkeepingException { ... }

等等。如果你认为你的API的用户可能想捕获任何一种“找不到记账事务”的异常,可以将其放在层次结构中的一个级别(但对我来说似乎不太合适)。

现在你可以这样写:

try {
    bookkeepingthingie();
} catch (BookkeepingException e) {
}

如果你不特别关心出错的具体原因,那么可以捕获更具体的异常,如果关心的话,甚至可以结合它们(如果找不到,执行这个操作;对于其他记账问题,执行这个操作 - 你可以使用层次结构来做到这一点;首先捕获更具体的异常,然后是一般的异常)。

英文:

No, this is good. The one downside is that you end up writing something like:

try {
   bookkeeperthingie();
} catch (AccountExpenseAlreadyExistsException |
   AccountExpenseNotFoundException | UsernameOrPasswordInvalidException |
   Blablabla | Morebla e) {
}

to avoid that, make sure your exceptions follow a hierarchy. You might for example have:

public class BookkeepingException extends Exception {
    public BookkeepingException(String msg) { super(msg); }
    public BookkeepingException(String msg, Throwable cause) { super(msg, cause); }
}

public class AccountExpenseAlreadyExistsException extends BookkeepingException { ... }

and so forth. If you find it particularly plausible that a user of your API would want to catch any kind of 'bookkeeping thingie not found' exception, make that a level in the hierarchy (but that that doesn't sound right to me).

Now you could write:

try {
    bookkeepingthingie();
} catch (BookkeepingException e) {
}

if you don't particularly care about the kind of thing that went wrong, and catch a more specific one if you do, or even combine them (if not found, do this. for any other bookkeeping issue, do this - you can do that with a hierarchy; catch the more specific one first, then the general one).

huangapple
  • 本文由 发表于 2020年8月7日 11:03:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63294576.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定