Error handling is one of the most important (and maybe least enjoying) things to do while developing. Most of the times error-prone methods will contain try/catch blocks to prevent the exception from slipping through. There might still be some unhandled exceptions though, in code blocks not "protected" by a try/catch block. Those can be handled on page or on application level.
Now let's see an example, writing the error to the trace (so it can be viewed and further investigated by a developer using a trace listener) in case of page level error handling and navigating to a user defined error page in case of application level exception handling.
On page
level :
protected void
Page_Error(object sender, EventArgs e) { // Retrieve the last error and write to the trace
Trace.Write(Server.GetLastError()); // Clear the last error
Server.ClearError();
} On
application level :
void
Application_Error(object sender, EventArgs e) { // Navigate to a user defined error page Server.Transfer("HandleError.aspx"); // Make sure to call Server.GetLastError() and Server.ClearError() in the error handling page }
11-08-2007 om 23:19
geschreven door Reinout Waelput 
|