This is what I use for my custom errors within my applications.
In the “Global.asax” I add the following code:
-
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
-
' Code that runs when an unhandled error occurs
-
Server.Transfer("Errors.aspx")
-
End Sub
-
Here is a slimed down version of my “Errors.aspx” page:
-
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Errors.aspx.vb" Inherits="Errors" %>
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head id="Head1" runat="server">
-
<title></title>
-
</head>
-
<body>
-
<form>
-
<div>
-
<asp:Label ID="LblMessagesAndErrors" runat="server" ForeColor="#e10000" Font-Bold="true" />
-
</div>
-
</form>
-
</body>
-
</html>
-
Here is a slimed down version of my “Errors.aspx.vb” page:
-
Partial Class Errors
-
Inherits System.Web.UI.Page
-
Dim MailFunctions As MailMan = New MailMan
-
-
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
-
Dim errMessage As String = ""
-
Dim errorCode As String = "n/a"
-
Dim appException As System.Exception = Server.GetLastError()
-
-
If (TypeOf (appException) Is HttpException) Then
-
Dim checkException As HttpException = CType(appException, HttpException)
-
errorCode = checkException.GetHttpCode
-
Select Case checkException.GetHttpCode
-
Case 403
-
errMessage &= "Error 777." & checkException.GetHttpCode & ": You are not allowed to view that page."
-
Case 404
-
errMessage &= "Error 777." & checkException.GetHttpCode & ": The page you have requested can't be found."
-
Case 408
-
errMessage &= "Error 777." & checkException.GetHttpCode & ": The request has timed out."
-
Case 500
-
errMessage &= "Error 777." & checkException.GetHttpCode & ": The server can't fulfill your request."
-
Case Else
-
errMessage &= "Error 777." & checkException.GetHttpCode & ": The server has experienced an error."
-
End Select
-
Else
-
errMessage &= "Error 777.0.000: The server has experienced an error."
-
End If
-
-
LblMessagesAndErrors.Text = errMessage & "<br /><p>We are sorry for this inconvenience.</p><p>Please try back.</p>"
-
-
Dim errEmailMessage As String = "<br/ > <br/ >" & Request.UrlReferrer.ToString() & "<br/ > <br/ >" & Now()
-
-
Try
-
MailFunctions.SendEmail("CroCrew@None.com", "[~~~] ERROR www.MySite.com ERROR [~~~]", "[" & errorCode & "] " & appException.ToString & errEmailMessage)
-
Catch ex As Exception
-
' just catching.
-
End Try
-
-
Server.ClearError()
-
End Sub
-
-
End Class
-
Every time an error outside of a try/catch is thrown it goes to this Error page and an email gets sent to me with information that I can use to start debugging the problem. Also, if the “Case Else” is used within the “Select Case checkException.GetHttpCode” I then can add to that case statement a new code if I want too.
I should say that I have a class that handles my sending of email called “MailMan”.
This along with trapping my code within “try/catch”s seems to handle all my custom error handling.
Hope this helps,
CroCrew~