Generic Error handling

samsmithnz

Senior Contributor
Joined
Jul 22, 2003
Messages
1,038
Location
Boston
I working hard at creating a generic error handler for a big application I'm working on. I'm a little confused about how to do this.

I've tryed adding Try..Catch statements that redirect to a Error.aspx page, but it's difficult to pass the error to the error page. Whenever I try to use the Server.GetLastError function, it only returns nothing.

I also tryed manually spitting out the error with response.write, and then server.transfer, but I get threading errors, I have to have a blank error page (which isn't really very good), and it seems overly cumbersome...

I've tryed using the defaultRedirect attribute in the web.config file, but I'm not sure if I'm still supposed to use the try catch statements or not.

can anyone give me some guidance/ideas?
 
You mean you want to catch an exception and still have a error written to the server's log? Hmm...never tried that. If you catch an error, can u save the error to a db or email it. Otherwise, i suppose you can not use the try catch block and then have your application error sub get the server.getlasterror
 
I want to catch the error, redirect to a generic error page and display the error there. Then all the errors are travelling through a central point and I can easily add email or event logging in a central place.
 
I normally have a global error handler, like the application_error sub in the global.asax file. I do all of my custom logging in here. Then my defaultredirect in my web.config takes over and displays the error page. So if you catch an exception, do a throw(ex).
 
So for example on my logon page I should have:
Code:
Try
    .....
Catch ex as Exception
    Throw ex
End Try

and then add a application_error to the global asmx file and add the line in the web.config to redirect to my error web page? How do I display the error in my error webpage?
 
Well, once you get to your error.aspx or whatever you name it, the server.getlasterror is done with. So, what you could do is,store the exception into the application bag. something like this...
sub application_error( byval e, blah)
dim newexception as exeception
newexception = Server.GetLastError()
HttpContext.Current.Application.Add("SiteException",newexception)
end sub

then in your error.aspx, set it to your exception
Dim myexception as exception
myexception = HttpContext.Current.Application.Get("SiteException")

then you can do what you want with it.
 
Back
Top