
Error Handling | 217
Error Handling
This is the Title of the Book, eMatter Edition
Copyright © 2008 O’Reilly & Associates, Inc. All rights reserved.
Structured Exception Handling
The most specific technique for exception handling, and the most useful in terms
of gracefully recovering from the exception, is structured exception handling.
Structured exception handling should be familiar to developers of Java and C++,
for which it is standard practice, but it is new to the Visual Basic .NET language.
Microsoft’s new language, C#, also provides built-in support for structured
exception handling.
In structured exception handling, you wrap code that may throw an exception in
a
Try...Catch block, as shown in the following code snippet:
'VB.NET
Try
' Code that may cause an exception
Catch ex As Exception
' Exception handling code
Finally
' Code executes whether or not an exception occurs
End Try
//C#
try
{
// Code that may cause an exception
}
catch (Exception ex)
{
// Exception handling code
Example 10-6. global.asax
<%@ Import Namespace="System.Web.Mail" %>
<script language="VB" runat="server">
Sub Application_Error(sender As Object, e As EventArgs)
Dim ex As Exception = Server.GetLastError( )
If Not ex Is Nothing Then
Dim Mail as New MailMessage( )
'Change the values below to valid email addresses
Mail.To = <valid email address>
Mail.From = <valid email address>
Mail.Subject = "aspnetian.com error"
Mail.Body ...