首先,定义MyAppCustomException
为抽象基类。
然后从它继承AppNotFoundCustomException
.
通过这种方式,您可以捕获应用程序中的所有异常,或者仅捕获特定异常。
以下是一些说明该概念的示例代码:
public abstract class MyAppCustomException : System.Exception
{
internal MyAppCustomException(string message)
: base(message)
{
}
internal MyAppCustomException(string message, System.Exception innerException)
: base(message,innerException)
{
}
}
public class AppNotFoundCustomException : MyAppCustomException
{
public AppNotFoundCustomException(): base("Could not find app")
{
}
}
这是一个客户端try/catch
示例:
try
{
// Do Stuff
}
catch(AppNotFoundCustomException)
{
// We know how to handle this
}
catch(MyAppCustomException) // base class
{
// we don't know how to handle this, but we know it's a problem with our app
}