创建自己的自定义异常有哪些最佳做法?

时间:2021-03-30 20:03:48

In a follow-up to a previous question regarding exceptions, what are best practices for creating a custom exception in .NET?

在前一个关于异常的问题的后续内容中,在.NET中创建自定义异常的最佳实践是什么?

More specifically should you inherit from System.Exception, System.ApplicationException or some other base exception?

更具体地说,您应该继承System.Exception,System.ApplicationException还是其他一些基本异常?

5 个解决方案

#1


16  

Inherit from System.Exception. System.ApplicationException is useless and the design guidelines say "Do not throw or derive from System.ApplicationException."

从System.Exception继承。 System.ApplicationException没用,设计指南说“不要抛出或派生自System.ApplicationException”。

See http://blogs.msdn.com/kcwalina/archive/2006/06/23/644822.aspx

见http://blogs.msdn.com/kcwalina/archive/2006/06/23/644822.aspx

#2


26  

In the C# IDE, type 'exception' and hit TAB. This will expand to get you started in writing a new exception type. There are comments withs links to some discussion of exception practices.

在C#IDE中,键入“exception”并单击TAB。这将扩展为您开始编写新的异常类型。有评论链接到一些异常实践的讨论。

Personally, I'm a big fan of creating lots of small classes, at that extends to exception types. For example, in writing the Foo class, I can choose between:

就个人而言,我是创建大量小类的忠实粉丝,其扩展到异常类型。例如,在编写Foo类时,我可以选择:

  1. throw new Exception("Bar happened in Foo");
  2. 抛出新的异常(“Bar发生在Foo”);
  3. throw new FooException("Bar happened");
  4. 抛出新的FooException(“Bar happen”);
  5. throw new FooBarException();
  6. 抛出新的FooBarException();

where

哪里

class FooException : Exception 
{
    public FooException(string message) ... 
}

and

class FooBarException : FooException 
{
    public FooBarException() 
        : base ("Bar happened") 
    {
    }
}

I prefer the 3rd option, because I see it as being an OO solution.

我更喜欢第三种选择,因为我认为它是一种OO解决方案。

#3


5  

There is a code snippet for it. Use that. Plus, check your code analysis afterwards; the snippet leaves out one of the constructors you should implement.

有一个代码片段。用那个。另外,之后检查您的代码分析;代码片段遗漏了您应该实现的构造函数之一。

#4


1  

I think the single most important thing to remember when dealing with exceptions at any level (making custom, throwing, catching) is that exceptions are only for exceptional conditions.

我认为在处理任何级别的异常(进行自定义,抛出,捕获)时要记住的最重要的事情是异常仅适用于特殊情况。

#5


1  

The base exception from where all other exceptions inherit from is System.Exception, and that is what you should inherit, unless of course you have a use for things like, say, default messages of a more specific exception.

所有其他异常从其继承的基本异常是System.Exception,这是您应该继承的,除非您当然使用诸如更具体异常的默认消息之类的东西。

#1


16  

Inherit from System.Exception. System.ApplicationException is useless and the design guidelines say "Do not throw or derive from System.ApplicationException."

从System.Exception继承。 System.ApplicationException没用,设计指南说“不要抛出或派生自System.ApplicationException”。

See http://blogs.msdn.com/kcwalina/archive/2006/06/23/644822.aspx

见http://blogs.msdn.com/kcwalina/archive/2006/06/23/644822.aspx

#2


26  

In the C# IDE, type 'exception' and hit TAB. This will expand to get you started in writing a new exception type. There are comments withs links to some discussion of exception practices.

在C#IDE中,键入“exception”并单击TAB。这将扩展为您开始编写新的异常类型。有评论链接到一些异常实践的讨论。

Personally, I'm a big fan of creating lots of small classes, at that extends to exception types. For example, in writing the Foo class, I can choose between:

就个人而言,我是创建大量小类的忠实粉丝,其扩展到异常类型。例如,在编写Foo类时,我可以选择:

  1. throw new Exception("Bar happened in Foo");
  2. 抛出新的异常(“Bar发生在Foo”);
  3. throw new FooException("Bar happened");
  4. 抛出新的FooException(“Bar happen”);
  5. throw new FooBarException();
  6. 抛出新的FooBarException();

where

哪里

class FooException : Exception 
{
    public FooException(string message) ... 
}

and

class FooBarException : FooException 
{
    public FooBarException() 
        : base ("Bar happened") 
    {
    }
}

I prefer the 3rd option, because I see it as being an OO solution.

我更喜欢第三种选择,因为我认为它是一种OO解决方案。

#3


5  

There is a code snippet for it. Use that. Plus, check your code analysis afterwards; the snippet leaves out one of the constructors you should implement.

有一个代码片段。用那个。另外,之后检查您的代码分析;代码片段遗漏了您应该实现的构造函数之一。

#4


1  

I think the single most important thing to remember when dealing with exceptions at any level (making custom, throwing, catching) is that exceptions are only for exceptional conditions.

我认为在处理任何级别的异常(进行自定义,抛出,捕获)时要记住的最重要的事情是异常仅适用于特殊情况。

#5


1  

The base exception from where all other exceptions inherit from is System.Exception, and that is what you should inherit, unless of course you have a use for things like, say, default messages of a more specific exception.

所有其他异常从其继承的基本异常是System.Exception,这是您应该继承的,除非您当然使用诸如更具体异常的默认消息之类的东西。