Customize Default ASP.NET Exception Handling
ASP.NET has a default exception-handling system, but it ain’t pretty — unless you take the time to customize it.
October 30, 2009
asp:feature companion
LANGUAGES: VB .NET
TECHNOLOGIES: Exception Handling
Customize Default ASP.NET Exception Handling
ASP.NET has a default exception-handling system, but itain't pretty - unless you take the time to customize it.
By Don Kiely
Like pretty much every development environment in thehistory of computing, ASP.NET has a default exception handling system, which isunlikely to be acceptable for any production application. Suppose you have thiscode in a Page's Load event procedure:
Dim x As Integer = 44
Dim y As Integer = 31
Dim z As Double
Dim zi As Double
z = x / y
zi = x y
lblResult.Text = "When x is " & x.ToString _
& " and y is" & y.ToString _
& ", x / y is" & z.ToString _
& ", and x yis " & zi.ToString & "."
Now suppose somehow the variable y in the previous code is0 instead of 31. Interestingly, the result of the floating-point divisionassigned to z is a special value of infinity, so the line z = x / y doesn'tthrow an error. But the line zi = x / y does throw an exception, which is nothandled in this procedure or anywhere else in the application. So the ASP.NETdefault behavior kicks in.
What the user sees depends on where the application isrunning. If the browser is running on the same machine as the application, whichis a common scenario during development, the user sees Figure 1. In many cases,this provides enough information to find what went wrong so you can fix theproblem.
Figure 1. With the defaultconfiguration out of the box, a user running an application with an unhandledexception gets a fair bit of information about what caused the problem,including a stack trace.
If the browser is running on a remote machine, such aswhen an application goes into testing or production and real users are hittingthe server, the same error will present the user with Figure 2.