简体   繁体   中英

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. 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). The exception handling part should be written in base class catch inside RunBase. 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 .

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. Handling exception normally happen at the UI level.

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

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.

Just remove the try catch from class B, if the exception occurs it will propergate up the call chain until it is handled. In this case you can handle the exception in RunBase using your existing try catch block.

Though in your example B isn't derived from your base class 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.
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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