문제

I have a base class, and I would like to catch all exceptions of the derived class within the base class, is this possible?

You won't know what the methods are from the derived class.

도움이 되었습니까?

해결책

You need to provide more details about your specific scenario. However if for example you have a base abstract class that provides a contract and you want to catch all possible exceptions thrown by derived classes when calling the base class contract you can do something like this:

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 { /* ... */ }
    }
}

다른 팁

By calling class you mean a derived class, or a non-related class calling methods from a class derived from your base?

I guess you can do that turning your base into a proxy class. See a dynamic proxy example.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top