有没有办法在没有Exception类的情况下抛出自定义异常

Wer*_*eit 30 c# asp.net exception

有没有什么方法在C#(即在.NET中)抛出自定义异常但没有编写所有代码来定义自己的派生自的异常类Exception

我正在考虑类似于Oracle PL/SQL中您可以编写的类似内容

raise_application_error(-20001, 'An arbitary error message');
Run Code Online (Sandbox Code Playgroud)

在任何地方.

Dan*_*eld 59

throw new Exception("A custom message for an application specific exception");
Run Code Online (Sandbox Code Playgroud)

还不够好?

如果相关,您还可以抛出更具体的异常.例如,

throw new AuthenticationException("Message here");
Run Code Online (Sandbox Code Playgroud)

要么

throw new FileNotFoundException("I couldn't find your file!");
Run Code Online (Sandbox Code Playgroud)

可以工作.

请注意,根据MSDN,您可能 应该这样做. throw new ApplicationException()

不定制Exception的主要缺点是调用者将更难以捕获 - 他们不会知道这是一般异常还是特定于您的代码而不对exception.Message属性进行一些时髦的检查.你可以做一些简单的事情:

public class MyException : Exception
{
    MyException(int severity, string message) : base(message)
    {
        // do whatever you want with severity
    }
}
Run Code Online (Sandbox Code Playgroud)

避免这种情况.

更新:Visual Studio 2015现在提供了一些Exception扩展类的自动实现 - 如果您打开快速操作和重构菜单上的光标: Exception,只需告诉它"生成所有构造函数".

  • @MetroSmurf想一想,这是一个例外,没有任何关于发生了什么的信息.最重要的是,如果不处理每个异常都不可能处理(`catch Exception`而不是`catch SpecificException`). (3认同)
  • 您不应该使用`ApplicationException`类,请参阅此处的备注:https://msdn.microsoft.com/en-us/library/system.applicationexception%28v=vs.110%29.aspx (2认同)

Dav*_*vid 9

Exception班是不是abstract,像大多数在.NET中定义的异常,需要string message在构造函数重载之一-因此,你可以使用现有的异常类型,但有一个自定义的消息.

throw new Exception("Something has gone haywire!");
throw new ObjectDisposedException("He's Dead, Jim");
throw new InvalidCastException(
    $"Damnit Jim I'm a {a.GetType().Name}, not a {b.GetType().Name}!");
Run Code Online (Sandbox Code Playgroud)

因为它使用已知的异常类型,所以它使第三方更容易扩展库,因为它们不需要MyArbitraryExceptioncatch语句中查找.

  • 我将不得不记住`InvalidCastException`! (6认同)
  • @David这是正确的,但所有的**异常**都应该是**DwightSchruteException("..")**LOL (2认同)

Fen*_*ton 5

简短的回答 - 没有.

有一个很好的理由来强制继承自定义异常; 人们需要能够处理它们.如果您可以在没有类型的情况下抛出自定义异常,则人们将无法捕获该异常类型.

如果您不想编写自定义异常,请使用现有的异常类型.


Mir*_*rko 5

在 C# 中创建自定义异常的一种简单方法是使用泛型类。如果您需要创建很多异常(即,如果您需要在单元测试中区分它们),这将大大减少代码行数。

首先创建一个简单的类,名为CustomException<T>

public class CustomException<T> : Exception where T : Exception
{
    public CustomException() { }
    public CustomException(string message) : base(message){ }
    public CustomException(string message, Exception innerException) : base(message, innerException){ }
    public CustomException(SerializationInfo info, StreamingContext context) : base(info, context){ }
}
Run Code Online (Sandbox Code Playgroud)

您可以根据需要(或需要)重写任意数量的构造函数和方法。为了创建新的异常类型,只需添加新的单行类:

public class MyCustomException : Exception { }
public class SomeOtherException : Exception { }
Run Code Online (Sandbox Code Playgroud)

如果您想引发自定义异常,请使用:

throw new CustomException<MyCustomException>("your error description");
Run Code Online (Sandbox Code Playgroud)

这使您的异常代码保持简单,并允许您区分这些异常:

try
{
    // ...
}
catch(CustomException<MyCustomException> ex)
{
    // handle your custom exception ...
}
catch(CustomException<SomeOtherException> ex)
{
    // handle your other exception ...
}
Run Code Online (Sandbox Code Playgroud)