none
Handling Exceptions inside an Exception Handler RRS feed

  • Question

  • Hi,

    Its quite common that an exception thrown in the normal code is handled in an Exception block and is logged for further investigation.
    But I wonder what if an exception is thrown inside another exception handler and so on. How this would be dealt?
    Please some one put some light on this.

    Thanks,
    Rockyl
    Thursday, January 21, 2010 6:56 PM

Answers

  • That depends on your code. There is no general magic answer.

    I would follow these rules:
      - Try not to throw exceptions inside catch block.
      - If you have to do that anyway, make sure you are catching such exceptions and handle them inside the catch block.

    I don't think that the way to make your app robust against exception in catch blocks is different than to make it robust against any 'normal' exception.
    Or do you think it is different? If yes, why? What is the problem?
    Are you worried that if you get exception report, you won't be able to reproduce the problem, because the outter exception is not part of that report? I would collect dumps in such case or attach debugger if that happens.

    -Karel

    Friday, January 22, 2010 6:20 PM
    Moderator

All replies

  • Just try it in a small example and you will see how it behaves - the latest exception will be 'in action', the first one will be lost (unless you specify it as inner exception - see http://msdn.microsoft.com/en-us/library/804f22sf.aspx).

    -Karel
    Thursday, January 21, 2010 9:09 PM
    Moderator
  • Hi, Rockyl:
        This can be easily tried out. Below is a small code sample to demostrate exception handling:

    using System;
    
    namespace ExceptionInCatch
    {
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    DivideByZeroException e0 = new DivideByZeroException("divided by zero");
                    throw e0;
                }
                catch (DivideByZeroException e0)
                {
                    try
                    {
                        Console.WriteLine(e0.Message);
                        Exception e1 = new Exception("exception inside the catch");
                        throw e1;
                    }
                    catch (Exception e2)
                    {
                        Console.WriteLine(e2.Message);
                    }
                }
                finally
                {
                    Console.WriteLine("Finally");
                }
            }
        }
    }
       
      And the output is:

    divided by zero
    exception inside the catch
    Finally

        If you remove the inside catch block, e0 will be handled while e1 will not.
        The finally will be executed here in either case. But its execution is not guaranteed. You can refer to constrained execution regions.

    Thanks,
    Sam
    • Edited by SamAgain Friday, January 22, 2010 3:32 AM refine
    Friday, January 22, 2010 3:29 AM
  • Hi Karel,

    I guess I hadn't put the question in right way in my earlier thread - My bad!! latest exception would be executed, thats obvious.
    But what I was looking for is, if in an Application an exception is occured inside another exception, how would a developer take care of it.
    Means how the application can be made robust in this behavior?

    Thanks,
    Rakesh
    Friday, January 22, 2010 5:44 PM
  • That depends on your code. There is no general magic answer.

    I would follow these rules:
      - Try not to throw exceptions inside catch block.
      - If you have to do that anyway, make sure you are catching such exceptions and handle them inside the catch block.

    I don't think that the way to make your app robust against exception in catch blocks is different than to make it robust against any 'normal' exception.
    Or do you think it is different? If yes, why? What is the problem?
    Are you worried that if you get exception report, you won't be able to reproduce the problem, because the outter exception is not part of that report? I would collect dumps in such case or attach debugger if that happens.

    -Karel

    Friday, January 22, 2010 6:20 PM
    Moderator
  • It was a general question that was bugging me from some time. And I just wanted to know if there could be any way we handle this better.

    Anyways Thanks,
    Rakesh
    Friday, January 22, 2010 6:33 PM