Can we catch exception from child class method in base class in C#?

后端 未结 2 1384
[愿得一人]
[愿得一人] 2021-01-18 14:16

In an interview interviewer asked me this question. Can we catch exception thrown by a child class method in base class? I said no, but he said yes it\'s possible. So I w

相关标签:
2条回答
  • 2021-01-18 14:24

    Here's a simple example in which a base class is catching an exception from a derived class.

    abstract class Base
    {
        // A "safe" version of the GetValue method.
        // It will never throw an exception, because of the try-catch.
        public bool TryGetValue(string key, out object value)
        {
            try
            {
                value = GetValue(key);
                return true;
            }
            catch (Exception e)
            {
                value = null;
                return false;
            }
        }
    
        // A potentially "unsafe" method that gets a value by key.
        // Derived classes can implement it such that it throws an
        // exception if the given key has no associated value.
        public abstract object GetValue(string key);
    }
    
    class Derived : Base
    {
        // The derived class could do something more interesting then this,
        // but the point here is that it might throw an exception for a given
        // key. In this case, we'll just always throw an exception.
        public override object GetValue(string key)
        {
            throw new Exception();
        }
    }
    
    0 讨论(0)
  • 2021-01-18 14:31

    Here you go:

    public class BaseClass
    {
        public void SomeMethod()
        {
            try
            {
                SomeOtherMethod();
            }
            catch(Exception ex)
            {
                Console.WriteLine("Caught Exception: " + ex.Message);
            }
        }
    
        public virtual void SomeOtherMethod()
        {
            Console.WriteLine("I can be overridden");
        }
    }
    
    public class ChildClass : BaseClass
    {
        public override void SomeOtherMethod()
        {
            throw new Exception("Oh no!");
        }
    }
    

    SomeMethod, defined on the base class, calls another method SomeOtherMethod of the same object and catches any exceptions. If you override SomeOtherMethod in some child class and it throws an exception, then this is caught in SomeMethod defined on the base class. The language used in your question is a little ambiguous (technically at runtime it is still the instance of ChildClass doing the exception handling) but I assume that this is what your interviewer was getting at.

    Another possibility (again, depending on the interpretation) is that an instance of a base class calls a method of a different object that inherits said base class, which throws an exception (and then catches the exception):

    public class BaseClass
    {
        public void SomeMethod()
        {
            var thing = new ChildClass();
            try
            {
                thing.ThrowMyException();
            }
            catch(Exception ex)
            {
                Console.WriteLine("Exception caught: " + ex.Message);
            }
        }
    }
    
    public class ChildClass : BaseClass
    {
        public void ThrowMyException()
        {
            throw new Exception("Oh no!");
        }
    }
    

    Here, when BaseClass.SomeMethod is called, an instance of a base class catches an exception thrown in another instance of a child class.

    0 讨论(0)
提交回复
热议问题