简体   繁体   English

处理基类异常

[英]Handle base class exception

I have a following C# scenario- I have to handle an exception in base class that actually occurs in the derived class. 我有一个以下的C#场景 - 我必须在基类中处理实际发生在派生类中的异常。 My base class looks like this: 我的基类看起来像这样:

public interface A
{
    void RunA();
}
public class Base
    {
        public static void RunBase(A a)
        {
            try
            {
                a.RunA();
            }
            catch { }
        }
    }

The derived class is as follows: 派生类如下:

public class B: A
{
        public void RunA()
        {
            try
            {
                //statement: exception may occur here
            }
            catch{}
    }
}

I want to handle the exception, lets say exception C, that occurs in B(at //statement above). 我想处理异常,比方说C,发生在B(在//语句上面)。 The exception handling part should be written in base class catch inside RunBase. 异常处理部分应该写在RunBase内的基类catch中。 How can this be done? 如何才能做到这一点?

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).
    }
}

How can this be done? 如何才能做到这一点?

What do you mean? 你什么意思? Just remove the try-catch block from RunA . 只需RunA 删除 try-catchRunA

Having said that, you need to make sure Class A knows how to handle the exception , this includes streamlining it to UI, logging, ... This is in fact rare for a base class. 话虽如此,您需要确保A类知道如何处理异常 ,这包括将其简化为UI,日志记录,......这对于基类来说实际上很少见 Handling exception normally happen at the UI level. 处理异常通常发生在UI级别。

public class B: A
{
        public void RunA()
        {
            try
            {
                // statement: exception may occur here
            }
            catch(Exception ex)
            {
                // Do whatever you want to do here if you have to do specific stuff
                // when an exception occurs here
                ...

                // Then rethrow it with additional info : it will be processed by the Base class
                throw new ApplicationException("My info", ex);
            }
    }
}

You also might want to throw the exception as it is (use throw alone). 您也可能希望按原样抛出异常(单独使用throw )。

If you dont need to process anything here, dont put try{} catch{}, let the exception bubble up by itself and be processed by the Base class. 如果你不需要在这里处理任何东西,不要把try {} catch {},让异常自己冒泡并由Base类处理。

Just remove the try catch from class B, if the exception occurs it will propergate up the call chain until it is handled. 只需从类B中删除try catch,如果发生异常,它将正确调用调用链,直到它被处理完毕。 In this case you can handle the exception in RunBase using your existing try catch block. 在这种情况下,您可以使用现有的try catch块在RunBase中处理异常。

Though in your example B isn't derived from your base class Base. 虽然在您的示例中B不是从您的基类Base派生的。 If you really want to handle a situation where an exception is thrown in a derived class in its parent you could try something like: 如果您真的想要处理在其父级派生类中抛出异常的情况,您可以尝试以下方法:

public class A
{
    //Public version used by calling code.
    public void SomeMethod()
    {
        try
        {
            protectedMethod();
        }
        catch (SomeException exc)
        {
            //handle the exception.
        }
    }

    //Derived classes can override this version, any exception thrown can be handled in SomeMethod.
    protected virtual void protectedMethod()
    {
    }

}

public class B : A
{
    protected override void protectedMethod()
    {
        //Throw your exception here.
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM