何时使用自定义异常与现有异常与通用异常 [英] When to use custom exceptions vs. existing exceptions vs. generic exceptions

查看:97
本文介绍了何时使用自定义异常与现有异常与通用异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试图找出正在写的一个图书馆,正确的异常形式是什么。我需要处理的一个例子是将用户登录到一个电台。他们通过扫描徽章来做到这一点。可能出现的问题包括:




  • 他们的徽章已停用

  • 他们没有

  • 扫描的徽章不存在于系统中

  • 他们已经登录到其他地方的其他站点

  • 数据库已关闭

  • 内部DB错误(有时如果徽章未正确设置)



使用此库的应用程序必须以这种方式处理这些异常。可能他们可能会决定只是说错误,或者他们可能想给用户更多有用的信息。在这种情况下最好的做法是什么?为每种可能性创建自定义异常?使用现有的例外?使用异常并传递原因(抛出新的异常(徽标被禁用); )?我认为这是前两者的混合,在适用的情况下使用现有的异常,并在需要时创建新的异常(并且在有意义的情况下分组异常)。

解决方案

你有两种异常。



那些特定于你的应用程序的那些,避免任何现有的异常。



应用程序特定的异常应该简化使用您的库的人的用例。您的应用程序中的3个特定异常是用户可以做的事情。第四个(徽章不存在)显然不是程序性的,但更严重。



看起来您有两个应用程序特定的错误:面向用户的事情和管理错误。



其他的是其他一些技术的一部分;即数据库错误。你可以 - 一般 - 忽略这些。如果数据库不可用,那么API会抛出错误,你可以通过你的库来启动它。



你也可以将它们包装具体的异常包含较低级的异常。如果有很多较低级别的技术,这有时是有帮助的。在你的情况下,它只是一个数据库。忽略它,让DB错误泡泡。


I'm trying to figure out what the correct form of exceptions to throw would be for a library I am writing. One example of what I need to handle is logging a user in to a station. They do this by scanning a badge. Possible things that could go wrong include:

  • Their badge is deactivated
  • They don't have permission to work at this station
  • The badge scanned does not exist in the system
  • They are already logged in to another station elsewhere
  • The database is down
  • Internal DB error (happens sometimes if the badge didn't get set up correctly)

An application using this library will have to handle these exceptions one way or another. It's possible they may decide to just say "Error" or they may want to give the user more useful information. What's the best practice in this situation? Create a custom exception for each possibility? Use existing exceptions? Use Exception and pass in the reason (throw new Exception("Badge is deactivated.");)? I'm thinking it's some sort of mix of the first two, using existing exceptions where applicable, and creating new ones where needed (and grouping exceptions where it makes sense).

解决方案

You have two species of exceptions.

Those that are specific to your application, where it's good to avoid any existing exceptions.

Your application-specific exceptions should simplify the use cases for the folks who use your libraries. 3 of your application specific exceptions are things users can do. The fourth (badge does not exist) is clearly not procedural, but far more serious.

It looks like you have two application-specific errors: user-oriented things and administrative mistakes.

The others are part of some other piece of technology; i.e., database errors. You can -- generally -- ignore these. If the DB isn't available, the API will throw errors and you can let those bubble up through your library.

You can also "wrap" these as an application-specific exception which contains a lower-level exception. This is sometimes helpful if there is a lot of lower-level technology. In your case, it's just a database. Ignore it and let the DB errors bubble through.

这篇关于何时使用自定义异常与现有异常与通用异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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