.net 异常

发布于 2024-09-20 00:56:08 字数 69 浏览 4 评论 0原文

我什么时候应该创建自己的自定义异常类而不是使用 .Net 提供的异常类?

我应该从哪个异常基类派生,为什么?

When should I create my own custom exception class rather than using a one provided by .Net?

Which base exception class should I derive from and why?

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

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

发布评论

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

评论(6

尽揽少女心 2024-09-27 00:56:09

到目前为止,答案看起来都不错,但我还要补充一点:

隐藏可能会暴露的实现细节,因为您需要处理这些异常。

例如,您不希望您的 UI 捕获 SQLException。您应该从数据访问代码中抛出您自己的异常,并让您的 UI 处理这些异常。如果您更改为非数据库提供程序来存储数据(例如 XML、文件系统等),则无需更改 UI 代码。

但是,只有当我在 UI 代码中显式处理它们时,我才会这样做。

The answers so far all look good, but I would also add:

To hide implementation details that might otherwise be exposed because you need to handle those exceptions.

E.g. You don't want your UI to catch SQLException. You should throw your own exceptions out of your data access code and let your UI deal with those. If you changed to a non database provider for data storage (e.g. XML, file system, etc), you would not need to change your UI code.

However, I would only do that if I was handling them in my UI code explicitly.

怪我闹别瞎闹 2024-09-27 00:56:09

如果我想向其中添加其他属性(例如变量、位置、用户名、返回记录数等),我会创建自己的异常类。

本质上,我会创建一个类来缩小错误范围以及如何重新修复-创建它。

编辑

哦,您可以将验证错误添加到异常类中,该异常类可以传递到前端以显示错误。

I'd create my own excpetion class if i wanted to add other properties to it such as variables, location, user name, num returned records etc.

essentially i'd create a class that would narrow down what the error was and how to re-create it.

edit

oh and you could add validation errors to an exception class that can be passed to the front end to show the errors.

爱的十字路口 2024-09-27 00:56:08

为什么要创建自己的异常?

您创建自己的异常,以便在抛出异常时可以捕获特定的异常,从而将它们与系统抛出的(未处理的)异常区分开来。

您应该从哪个类派生它?

早些时候,从 ApplicationException 类派生自定义异常是标准做法,但随着时间的推移,MS 建议发生了变化,鼓励开发人员从System.Exception 本身而不是 ApplicationException

Why create your own exception?

You create your own exception so that when you throw them, you can have specific catches and hence differentiate them from system thrown (unhandled) exceptions.

What class should you derive it from?

Earlier, it was standard practice for custom exceptions to be derived from ApplicationException class but over time, MS recommendations have changed encouraging developers to derive from System.Exception itself rather than ApplicationException

揽清风入怀 2024-09-27 00:56:08

这可能看起来有点明显,但当没有内置异常有意义时,您应该创建一个异常。通常我会为我正在开发的库定义一个基本异常。

public class MyLibraryException : Exception
{
    // .....
}

然后我将为使用该库时可能出现的情况创建一个例外。

public class SomethingHorribleException : MyLibraryException 
{
    // .....
}

然后客户端总是可以知道我的库将抛出继承 MyLibraryException 的内容。

This may seem a bit obvious but you should create an exception when no built in exceptions make sense. Typically I will define a base exception for a library I am working on.

public class MyLibraryException : Exception
{
    // .....
}

Then I will create an exception for situations that may arise when using the library.

public class SomethingHorribleException : MyLibraryException 
{
    // .....
}

Then the client can always know that my library will throw something that inherits MyLibraryException.

树深时见影 2024-09-27 00:56:08

异常处理的目标应该是清楚地了解出了什么问题。因此,只要提供的异常不能提供清晰的理解,您就应该定义自己的异常。

例如,我正在编写一个将阿拉伯数字转换为罗马数字的应用程序。本申请中的限制之一是罗马数字必须落在0<0的范围内。 x < 4000.

private string ToRoman(int number)
{
    if (number > 0 && number < 4000)
    {
        //ConvertHere
    }
    else
    {
        //Custom exception used for readability purposes.
        throw new NumeralOutOfRangeException();
    }
}

至于使用哪个基类,微软建议用户定义的异常子类Exception。 (http://msdn.microsoft.com/en-us/library/seyhszts .aspx)

The goal of exception handling should be to create a clear understanding of what went wrong. Therefore, you should define your own exception any time the provided exceptions do not provide that clear understanding.

For example, I'm writing an application which converts Arabic Numerals to Roman Numerals. One of the constraints in this application is that Roman Numerals must fall within the range 0 < x < 4000.

private string ToRoman(int number)
{
    if (number > 0 && number < 4000)
    {
        //ConvertHere
    }
    else
    {
        //Custom exception used for readability purposes.
        throw new NumeralOutOfRangeException();
    }
}

As for which base class to use, Microsoft recommends that user defined exceptions subclass Exception. (http://msdn.microsoft.com/en-us/library/seyhszts.aspx)

淡写薰衣草的香 2024-09-27 00:56:08

创建自己的异常的原因之一是能够隔离它们以进行捕获。例如:

try {
// do stuff
} catch (MyCustomException e) {
// handle this known exception
}

所有其他异常都可以冒泡并在另一个级别进行处理。

One reason to create your own exception is to be able to isolate them for catching. For example:

try {
// do stuff
} catch (MyCustomException e) {
// handle this known exception
}

All other exceptions can bubble up and be handled at another level.

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