如何知道何时使用现有异常或编写自定义异常?

发布于 2024-07-24 10:13:20 字数 774 浏览 12 评论 0原文

感谢您对此的意见问题,我决定让我的 Create() 方法抛出异常,这样正如 Jon Skeet 所说,你不必在任何地方处理它们,而可以让它们冒泡,似乎是大型应用程序的最佳方法。

所以现在我使用以下代码创建类的实例:

try
{
    SmartForms smartForms = SmartForms.Create("ball");
    smartForms.Show();
}
catch (CannotInstantiateException ex)
{
    Console.WriteLine("Item could not be instantiated: {0}", ex.Message);
}

自定义异常:

using System;

namespace TestFactory234.Exceptions
{
    class CannotInstantiateException : Exception
    {

    }
}

如何知道要使用哪个异常类?

在上面的实例中,我创建了自己的异常,因为我不知道在哪里获取“所有系统异常”的列表,或者是否有一个“无法实例化对象”,或者它是否有其他含义来使用它,等等。对我来说,选择异常类型总是看起来这样一个<强>任意过程,所以创建我自己的过程似乎是最好的想法。

或者我错过了一些关于例外的事情吗? 决定使用哪种异常类型还涉及哪些其他含义?

Thanks for the input on this question, I've decided to go with making my Create() method throw exceptions so as Jon Skeet said, you don't have to handle them everywhere and can just let them bubble up, seems the best approach for larger applications.

So now I create instances of my classes with this code:

try
{
    SmartForms smartForms = SmartForms.Create("ball");
    smartForms.Show();
}
catch (CannotInstantiateException ex)
{
    Console.WriteLine("Item could not be instantiated: {0}", ex.Message);
}

custom exception:

using System;

namespace TestFactory234.Exceptions
{
    class CannotInstantiateException : Exception
    {

    }
}

How do I know which Exception class to use?

In the above instance, I created my own Exception since I don't know where to get a list of "all system exceptions" or if there is one for "not being able to instantiate an object" yet or if it has some other meaning to use it, etc. Choosing an exception type to me has always seems such an arbitrary process, so creating my own seems to be the best idea in general.

Or am I missing something about exceptions? What other implications involved in deciding which Exception type to use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦太阳 2024-07-31 10:13:20

如果无法创建对象的原因是 Create 的参数无效,则可能应该抛出 ArgumentException。 但是,如果您确实希望能够单独处理此类异常,那么您始终可以创建从 ArgumentException 派生的我们自己的类。 (你确定你要?)

If the reason you can't create the object is because the argument to Create was invalid, you should probably throw an ArgumentException. However, you could always create our own class derived from ArgumentException if you really want to be able to handle that kind of exception separately to others. (Are you sure you want to?)

懵少女 2024-07-31 10:13:20

为什么创建自定义异常?非常详细地解释了为什么以及何时使用自定义异常。

Why Create Custom Exceptions? explains in pretty good detail why and when to use the custom exceptions.

一绘本一梦想 2024-07-31 10:13:20

就此主题撰写了一篇完整的博客文章,您可能会觉得有趣

总之,不要编写自定义异常类,除非您实际上希望有人捕获该类型并对其执行操作。

Wrote an entire blog post on this subject that you may find interesting

In summary, don't write a custom exception class unless you actually expect someone to both catch and act on the type.

泪之魂 2024-07-31 10:13:20

当能够捕获并知道发生的特定异常情况可能有用时,创建一个新类型。 知道找不到文件而不是通用 IO 异常有用吗?

Create a new type when it might be useful to be able to catch and know a specific exceptional case occured. Is it useful to know that a File wasnt found rather than a generic IO exception ?.

谈情不如逗狗 2024-07-31 10:13:20

您将创建一个自定义异常类型来提供更多上下文信息或错误含义,否则您将依赖运行时生成的异常类型。 例如,像 System.DivideByZero 异常这样的异常在应用程序中冒泡到顶部时可能并不明显。 相反,除了上述“DivideByZero”错误之外,您还可以创建自定义异常来提供更多上下文信息。

有关不同运行时生成的异常的参考,请查看 MSDN 的系统命名空间。 这不是一个详尽的列表,因为异常可以由本机代码生成,也可以从第三方库生成。

You will create a custom exception type to provide more contextual information or meaning to the error otherwise you will rely on the runtime generated exception types. For instance an exception like System.DivideByZero exception may not be obvious when it bubbles up to the top in an application. Instead, you could create a custom exception to provide more contextual information in addition to the above 'DivideByZero' error.

For reference on the different runtime generated exceptions, please have a look at MSDN's system namespace. This is not an exhaustive list since exceptions can be generated by native code and also from third party libraries.

北凤男飞 2024-07-31 10:13:20

我认为查找现有异常的好地方是在帮助文件中...如果您查找 Exception 类的帮助,概述页面上应该有一个派生类的列表。

如何决定是创建一个新异常(从 Exception 派生),还是从现有异常继承,取决于异常的含义。

正如 Jon 所说,如果您的代码对 Create 方法的参数进行了一些验证,您可能希望从 ArgumentException 派生一个异常(例如可能是一个 如果指定的 ID 不存在,则 ArgumentNonExistentEntityException(尽管这有点啰嗦)。

如果您创建的异常在概念上没有从已存在的异常中“继承”其含义,那么只需厚颜无耻地为您的库创建一个新异常即可。

I think a good place to find existing exceptions would be in the help file... if you look up the help for the Exception class, there should be a list of derived classes on the overview page.

How to decide whether to create a new one (derived from Exception), or inherit from an existing one depends on what the exception means.

As Jon says, if your code does some validation on the argument to the Create method, you may want to make an exception derived from ArgumentException (for example maybe an ArgumentNonExistentEntityException if the specified ID does not exist although that is a bit of a mouth full).

If the exception you are creating does not conceptually 'inherit' its meaning from an exception that already exists, just unashamedly create a new one for your library.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文