Ana*_*nya 4 c# inheritance exception-handling
我有一个以下的C#场景 - 我必须在基类中处理实际发生在派生类中的异常.我的基类看起来像这样:
public interface A
{
void RunA();
}
public class Base
{
public static void RunBase(A a)
{
try
{
a.RunA();
}
catch { }
}
}
Run Code Online (Sandbox Code Playgroud)
派生类如下:
public class B: A
{
public void RunA()
{
try
{
//statement: exception may occur here
}
catch{}
}
}
Run Code Online (Sandbox Code Playgroud)
我想处理异常,比方说C,发生在B(在//语句上面).异常处理部分应该写在RunBase内的基类catch中.如何才能做到这一点?
public class Base
{
public static void RunBase(A a)
{
try
{
a.RunA();
}
catch(SomeSpecialTypeOfException ex)
{
// Do exception handling here
}
}
}
public class B: A
{
public void RunA()
{
//statement: exception may occur here
...
// Don't use a try-catch block here. The exception
// will automatically "bubble up" to RunBase (or any other
// method that is calling RunA).
}
}
Run Code Online (Sandbox Code Playgroud)