locked
Why do we rethrow the exception RRS feed

  • Question

  • User43093768 posted
    In the Enterprise Library Exception Block why do we write this line:

    catch(System.DivideByZeroException ex)
                {
                    bool rethrow = ExceptionPolicy.HandleException(ex,"Divide By Zero Policy");

                    if(rethrow)
                    {
                        throw;
                    }

                }

    Monday, May 16, 2005 2:53 PM

All replies

  • User-94950375 posted

    You haven't handled the exception fully (or at all) in your policy, but as you've caught it, the exception no longer exists so rethrowing it just makes sure it will ripple up the stack.  If you don't rethrow it you will simply cause the exception to disappear.

    Within your policy file, you can return 1 of 3 choices by specifying  a PostHandlingAction

  • None. You have completely recovered from the exception, very rare
  • NotifyRethrow. Rethrow the same exception.
  • ThrowNewException Throw a completely different exception

    Of course you don't have to rethrow the exception if you can implement a recovery plan. The chief purpose of the ExceptionHandler block is to give you a nice mechanism to report exceptional conditions, it is still up to your application to deal with the exception retry logic.

    Cheers
    James

Monday, May 16, 2005 3:23 PM
  • User43093768 posted
    Thanks for the response. The problem is when I rethrow the exception it is not getting caught even by the Generaal Exception handler.


    Like:

    if(rethrow)
    {
    throw;
    }


    catch(Exception ex)
    {
        It never comes in  here.
    }

    Monday, May 16, 2005 3:46 PM
  • User-94950375 posted

    The penny drops, when you throw an exception you are thrown out of the exception handling block, you are probably doing code something like this

    public void Foo()
    {
        //this code will not work
       
    try 
        {
            
    throw new DivideByZeroException("I am here");
        }
        catch (DivideByZeroException ex)
        {
           
    bool rethrow = ExceptionPolicy.HandleException(ex,"Divide By Zero Policy");
            if(rethrow)
           {
                throw;
           }
        }
       catch (Exception)
       {
          // you will never get here
          Console.WriteLine("I am here too");
       }
    }

    Contrast that with thsi where yo nest the try blocks (not that I'm suggesting nested try blocks are a good idea, but it illustrates the point)

    public void Foo1()
    {
        //this will
        try
       {
           
    try 
           {
                throw new DivideByZeroException("I am in inner catch block");
           }
           catch (DivideByZeroException ex)
          {
               bool rethrow = ExceptionPolicy.HandleException(ex,"Divide By Zero Policy");
               if(rethrow)
              {
                   throw;
              }
          }
       }
       catch (Exception ex)
       {
           // you will however get to here
           Console.WriteLine("I am in outer catch block");
        }
    }

    Cheers
    James

     

    Monday, May 16, 2005 4:20 PM
  • User43093768 posted
    Okay I understand what you are saying. THanks,



    Monday, May 16, 2005 5:12 PM