如何决定创建受检查的排除或未检查的异常 [英] How does one decide to create a checked excpetion or an unchecked exception

查看:102
本文介绍了如何决定创建受检查的排除或未检查的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道一个人怎么知道创建并抛出一个受检查的异常或未受检查的异常。

I want to know how does one know to create and throw a checked exception or an unchecked exception.

例如,我有一个服务,它需要一些数据并进行验证使用之前。验证期间,某个字段不符合规则,我想抛出一个异常,说ValidationException()。我怎么知道应该决定要检查还是不检查。

For example I have a service which takes some data and validates it before using it. During validation a certain field did not meet the rules and I want to throw an exception say ValidationException(). How will I know of decide it should be checked or unchecked.

在另一种情况下,我从代码中调用了外部Web服务,例如google stock api。假设我的超时时间为3秒。如果时间到了,我想抛出异常,请说BackendException()。我怎么知道应该是检查异常还是非检查异常。

In another case I am calling an external web service from my code for example the google stock api. Let's assume i have a timeout of 3 seconds. If the time exprires i want to throw an exception say BackendException(). How will I know if it should be a checked exception or an unchecked exception.

预先感谢。

推荐答案

可能会有不同的意见,但我要说的是,呼叫者应如何处理该异常:

There might be different opinions but I'd say the difference is how the caller should handle that exception:


  • 如果要确保调用者通过执行某些操作(记录,尝试恢复等)或重新抛出来处理该异常,请使用已检查的异常。 ValidationException 就是一个例子:如果数据无效,则调用者必须处理该错误,例如通过告诉某人修复数据或尝试其他方法。 (请注意,可能存在未经检查的验证异常,例如 javax.validation.ValidationException 。可能有多种原因使这些未经检查,例如,如果框架不允许/不支持检查异常,或者是否有中央处理程序仍会捕获它们,这样开发人员就不必费心了。)

  • 它不希望强制调用方处理通常不应该抛出(例如编程错误等)使用未经检查的异常。例如,可能总是令人恐惧的 NullPointerException :它不应发生您要使用的内容为null的情况,因此可以将其视为编程错误。如果某些内容可能为空,但不应为空,则可能要使用检查异常。



    请注意,某些库/方法使用 IllegalArgumentException ,这是未经检查的异常。如果抛出此异常,通常会出现编程错误,因为这违反了方法的约定(例如,参数值不得为负),并且调用者应自行修复代码或自行进行检查。

  • If you want to make sure the caller handles the exception either by doing something (logging, trying to recover etc.) or rethrowing then use a checked exception. An example would be said ValidationException: if the data is invalid the caller should have to handle that, e.g. by telling someone to fix the data or by trying something else. (Note that there may be unchecked validation exceptions such as javax.validation.ValidationException. There can be various reasons for making those unchecked, e.g. if a framework doesn't allow/support checked exceptions or if some central handler will catch them anyway so the developer doesn't have to bother.)
  • It you don't want to force the caller to handle exceptions that normally should not be thrown (e.g. programming errors etc.) use an unchecked exception. An example for this might be the always dreaded NullPointerException: it should not happen that something you want to use is null so it might be considered a programming error. If something could be null but should not you might want use a checked exception instead.

    Note that some libraries/methods use IllegalArgumentException which is an unchecked exception. If this exception is thrown there's normally a programming error in that the contract of a method (e.g. the parameter value must not be negative) is violated and that the caller should fix the code or do some checks himself.

另一种观点可能是:是否在某些情况下会引发异常?预期的异常(仍然意味着发生了某种错误)将被检查为异常,因为这样您就可以与调用方进行通信,告知呼叫者在某些情况下(例如,如果数据无效)他应该期望这些异常被抛出。如果异常是意外的,则不应强迫调用者处理此类异常,因为您根本不希望引发该异常-因此,它将是未经检查的异常。

Another point of view might be: is the exception expected to be thrown in some cases or not? Expected exceptions (that still mean some kind of error happened) would be checked exceptions since that way you'd communicate to the caller that he should expect those exceptions being thrown in some cases (e.g. if the data is invalid). If the exception is unexpected you should not force the caller to handle such an exception since you'd not expect it to be thrown at all - thus it would be an unchecked exception.

这篇关于如何决定创建受检查的排除或未检查的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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