Error Handling in ASP.NET Page

Topics: Exception Handling Application Block
Nov 15, 2008 at 11:22 PM
I am using Microsoft Enterprise library Exceptional Handling library in my ASP.NET page. The library works fine if I have it in
                Try{

               {
                catch(Exception ex){
                       
bool rethrow = ExceptionPolicy.HandleException(ex, "Replace Policy");
                     
if (rethrow){throw;}
                      }

What if some one doesn't have try catch block, how do I catch the errors in Global.asax file in Application_Error sub routine.

Please help.

 

Nov 16, 2008 at 6:39 AM
Add the HandleException statement in the Application_Error event:

protected

void Application_Error(object sender, EventArgs e)
{
    
Exception ex = HttpContext.Current.Server.GetLastError();
    
bool rethrow = ExceptionPolicy.HandleException(ex, "ExceptionPolicy");

    
if (rethrow)
    {
        
throw;
    }
}

 



Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com

Nov 16, 2008 at 2:06 PM
Hi Sarah Urmeneta

I have the followig in the global.asax

void

 

Application_Error(object sender, EventArgs e)

 

{

 

Exception ex = HttpContext.Current.Server.GetLastError();

 

 

bool rethrow = Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.HandleException(ex, "ExceptionPolicy");

 

 

if (rethrow)

 

{

 

throw;

 

}

}


But that the following message    A throw statement with no arguments is not allowed outside of a catch clause.


Thanks

Nov 16, 2008 at 2:51 PM
Ooops, sorry about that.  You're only allowed to use the throw statement without specifying the exception to throw if you used it inside a try catch statement.  You can put the GetLastError method inside a try block and add the HandleException inside a catch block or you can just add these 2 lines inside the event,

Exception ex = HttpContext.Current.Server.GetLastError();
ExceptionPolicy.HandleException(ex, "Exception Policy");


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com

Nov 18, 2008 at 12:23 PM
Hi Sarah,

How do I capture handling instance GUID from  ExceptionPolicy.HandleException(ex, "Exception Policy"); so I can display that to the user.

Thanks
Yassin