为什么要创建自定义的异常?

为什么要创建自定义的异常?

问题描述:

为什么我们需要创建 .NET?

具体海关例外允许你来隔离不同的错误类型的catch语句。共同构建异常处理是这样的:

Specific customs exceptions allow you to segregate different error types for your catch statements. The common construct for exception handling is this:

try
{}
catch (Exception ex)
{}

这捕获的所有的异常类型无关。但是,如果你有自定义异常,你可以有不同的处理程序为每种类型的:

This catches all exceptions regardless of type. However, if you have custom exceptions, you can have separate handlers for each type:

try
{}
catch (CustomException1 ex1)
{
    //handle CustomException1 type errors here
}
catch (CustomException2 ex2)
{
    //handle CustomException2 type errors here
}
catch (Exception ex)
{
    //handle all other types of exceptions here
}

人机工程学,具体的例外允许您控制在你的异常处理更精细的水平。这样做的好处是共享的不仅是自定义异常,但在.NET系统库中的所有其他异常类型为好。

Ergo, specific exceptions allow you a finer level of control over your exception handling. This benefit is shared not only by custom exceptions, but all other exception types in the .NET system libraries as well.