简体   繁体   中英

How to override Catch(Exception) in Parent (Caller) method?

Is there any way to force that the caller could override the catch ?

ie

void ParentMethod()
{
  try 
  {   
      child_method(); 
  }
  catch(Exception e)
  {
      MessageBox.Show("parent");
  }
}

void child_method()
{
  try 
  {   
      smth(); 
  }
  catch(Exception e)
  {
      MessageBox.Show("child");
  }
}

so, when smth throws exception, instead of "child", i could override that catch with parent, and show ie parent .

Note 1: I am in a case, where I don't control child . However, for the sake of the answer completion, you can still answer for the case when child is controlled.

Note 2: I nowhere mentioned that I have classes or inheritance. Please avoid answers for virtual/override things. I know that.

There's not a way to enforce that a called method must rethrow any raised exceptions. The calling method is free to swallow whatever exception it wants.

That said, the way to catch the exception in the parent method also is to either rethrow the original exception or a new exception:

void child_method()
{
  try 
  {   
      smth(); 
  }
  catch(Exception e)
  {
      MessageBox.Show("child");
      //or: throw new Exception("Exception thrown in child");
  }
}

Short answer is no .
But there I guess you misunderstood few things about Object Oriented Programming.
So you can override a method with another method with same name and different prototype if you want more configuration or different behaviour. You can inherite from a class and override methods to share a common part and adjust whatever you want.

Ok so there you want a common part parent that would contain the try catch section.
So there you got a really different approach from what you did, but if I understood well quite suite what you want.

using System;

namespace ConsoleApp4
{
    class Program
    {
        static void Main(string[] args)
        {
            VirtualTest test1 = new Test1();
            test1.ParentMethod();
            VirtualTest test2 = new Test1();
            test2.ParentMethod();
            Console.ReadKey();
        }
    }

    abstract class VirtualTest
    {
        public void ParentMethod()
        {
            try
            {
                ChildMethod();
            }
            catch
            {
                Console.WriteLine("parent");
            }
        }

        protected abstract void ChildMethod();
    }

    class Test1 : VirtualTest
    {
        protected override void ChildMethod()
        {
            try
            {
                throw new ArgumentException("A message");
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
                throw; //Here is a way to handle more specificly some
                //errors but still throwing and keeping the original stacktrace
            }
        }
    }

    class Test2 : VirtualTest
    {
        protected override void ChildMethod()
        {
            throw new NotImplementedException();
        }
    }
}

In that case you also have more control of what you're catching at which level and you can decide wether you want to deal with it or not.
The finer level you handle the Exception the better. Also in that example Parents are actually parents and Children are actually childrend in a POO way.

If the child method's code is also owned/controlled/has access to, by the person/group who owns/controls the code for parent method, then you can do few things -

  1. if there's always a parent method to call this child method and that, child method is not going to be on its own (by your design), you can ignore the try-catch in child and have the parent handle whatever they want to.

  2. If (not #1) then its true from the above comment .

Ideally, and this is purely from my experience and opinion, if a method needs try-catch (based on what it does would qualify for it), catch block should catch the exception, do something like log it, and throw unless thats the last calling method.

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