我的自定义Exceptions应该继承与它们相似的异常还是仅继承Exception? [英] Should my custom Exceptions Inherit an exception that is similar to them or just inherit from Exception?

查看:120
本文介绍了我的自定义Exceptions应该继承与它们相似的异常还是仅继承Exception?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在应用程序中创建一些自定义异常。

I am creating some custom exceptions in my application.

如果我在测试参数的状态后抛出了异常,或者我遇到了一个异常在检测到int在适当范围内后被抛出,我的异常应该继承ArgumentException和IndexOutOfRangeException还是应该只继承Exception?

If I have an exception that gets thrown after testing the state of an argument, Or I have an Exception that gets thrown after testing that an int is within the proper range, should my exceptions inherit ArgumentException and IndexOutOfRangeException or should they just inherit Exception?

推荐答案

由于继承用于指定要捕获的异常,因此您在做出决定时应首先尊重这一点。

Since inheritance is used to specify which exceptions to catch, you should respect this primarily when taking a decision.

认为带有附加信息的IOException或ArgumentException除了ArgumentOutOfRangeException或ArgumentNullException。

Think of an IOException which carries additional information, or a ArgumentException other than ArgumentOutOfRangeException or ArgumentNullException.

这篇关于我的自定义Exceptions应该继承与它们相似的异常还是仅继承Exception?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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