Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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 want to know is this possible at all and if yes please give me any practical example. You don't have to call base class method. Thanks.

like image 484
user3729645 Avatar asked Jun 27 '14 17:06

user3729645


2 Answers

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();
    }
}
like image 181
Timothy Shields Avatar answered Nov 19 '22 23:11

Timothy Shields


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.

like image 5
Ant P Avatar answered Nov 20 '22 00:11

Ant P