ASP.NET: Error Handling Tips And Tricks Part 2 – Error Handling, Sending SMTP Mail and Custom Errors

.NET now has unified error handling across all languages and components. The common language runtime provides many great ways to handle exceptions. It’s now possible have a C# component throw an exception and then catch it in a VB component and bubble it up the stack. Errors work entirely the same way - It’s a very clean mechanism. When an error does occur, because we have managed code, we get a lot more information about specifics. Even without having debug symbols, we can figure out what the call stack was. You can know what line it was on without debug symbols, but you are able to know where it was called from, etc. That’s even on generic retail production systems. It makes it a lot easier to diagnose after the fact what failed on your system.

u Global application event raised if unhandled exception occurs

Ø Provides access to current Request

Ø Provides access to Exception object

Ø Enables developer to log/track errors

u Cool Tip:

Ø Use new EventLog class to write custom events to log when errors occur

Ø Use new SmtpMail class to send email to administrators

<%@ Import Namespace=“System.Web.Mail" %>

<script language="VB" runat=server>

    Sub Application_Error(sender as Object, e as EventArgs)

        Dim MyMessage as New MailMessage

        MyMessage.To = “someone@microsoft.com"

        MyMessage.From = "MyAppServer"

        MyMessage.Subject = "Unhandled Error!!!"

        MyMessage.BodyFormat = MailFormat.Html

        MyMessage.Body = "<html><body><h1>" & Request.Path & _

                "</h1>" & Me.Error.ToString() & "</body></html>"

        SmtpMail.Send(MyMessage)

    End Sub

</script>

In addition to writing the NT event log, you can also send email to an administrator when an error occurs.

The code here is pretty basic but very useful because it will ping an administrator when an error occurs. You see that we just Dim and instantiate a new MailMessage. Then just set some basic properties and send the message. It’s very straight-forward, but quite powerful too.

We don’t want customers to see ugly error messages, instead, we may want to show the user a custom page that says the website is undergoing maintenance, or something to that effect. So, ASP.NET provides a mechanism to do this called custom errors without having to write any code. Custom errors hides the error message for you.

<configuration>

  <system.web>

    <customErrors mode=“remoteonly”

                  defaultRedirect=“error.htm”>

      <error statusCode=“404”

             redirect=“adminmessage.htm”/>

      <error statusCode=“403”

             redirect=“noaccessallowed.htm”/>

    </customErrors>

  </system.web>

</configuration>

ASP.NET provides a way to configure inside an XML file a default error redirect whenever an unhandled exception occurs. In this sample code, you can see that whenever an unhandled error occurs, I want to go to the error.htm page.

Also, you can see that I set check status codes, such as 404. In this case, I can show an adminmessage.htm page stating that the page is not found. Additionally, I can check for 403 errors and show a page stating that the user does not have privileges. You can configure based on any of the error status codes that get generated.

You can configure the custom errors to be On, Off, or RemoteOnly. On means everyone sees it. Off means no one sees the custom errors. RemoteOnly means that any off the box will see the custom error page and anyone on the box will see the real stack trace. This is great for testing.