c# – 从类的基础捕获异常
作者:互联网
我有一个基类,我想在基类中捕获派生类的所有异常,这可能吗?
您不会知道派生类的方法是什么.
解决方法:
您需要提供有关特定方案的更多详细信息.但是,例如,如果您有一个提供契约的基本抽象类,并且您希望捕获派生类在调用基类契约时抛出的所有可能异常,则可以执行以下操作:
public abstract class Base
{
protected abstract void InternalFoo();
protected abstract void InternalBar();
public void Foo()
{
try { this.InternalFoo(); }
catch { /* ... */ }
}
public void Bar()
{
try { this.InternalBar(); }
catch { /* ... */ }
}
}
标签:c,exception-handling,base-class 来源: https://codeday.me/bug/20190701/1343075.html