Home/ASP.NET Forums/Advanced ASP.NET/Microsoft Patterns and Practices EntLib/Why do we rethrow the exception

Why do we rethrow the exception RSS

4 replies

Last post May 16, 2005 05:12 PM by azamsharp

James
My Blog: http://www.wintersfamily.plus.com/blogs/JamesWinters
  • azamsharp

    azamsharp

    Contributor

    2831 Points

    4524 Posts

    Re: Why do we rethrow the exception

    May 16, 2005 03:46 PM|azamsharp|LINK

    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.
    }

    ASP.NET articles and videos
    HighOnCoding
  • jcw14

    jcw14

    Member

    340 Points

    577 Posts

    Re: Why do we rethrow the exception

    May 16, 2005 04:20 PM|jcw14|LINK

    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

     

    James
    My Blog: http://www.wintersfamily.plus.com/blogs/JamesWinters
  • azamsharp

    azamsharp

    Contributor

    2831 Points

    4524 Posts

    Re: Why do we rethrow the exception

    May 16, 2005 05:12 PM|azamsharp|LINK

    Okay I understand what you are saying. THanks,



    ASP.NET articles and videos
    HighOnCoding
  • ‹ Previous Thread|Next Thread ›