ASP 101 - Active Server Pages 101 - Web03
The Place ASP Developers Go!

Windows Technology Windows Technology
15 Seconds
ASP 101
ASP Wire
VB Forums
VB Wire
internet.commerce internet.commerce
Partners & Affiliates
ASP 101 is an site
ASP 101 is an site
Internet News
Small Business
Personal Technology

Corporate Info
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash

 Top ASP 101 Stories Top ASP 101 Stories
What is ASP?
VBScript Classes: Part 1 of N
Migrating to ASP.NET

Server-Side vs. Client-Side Scripting
Show All Tips >>
ASP 101 RSS Feed ASP 101 Updates

Quick Tips

Be Sure to Handle Errors to Continue Script Execution

I recently spoke with a developer who was upset that the redundancy he built into his Web application had failed him. As a safeguard against database failure, he had written a script which wrote new entries to a log file on the local file system as well as adding them to the database. It was a good idea... unfortunately the implementation wasn't so good.

The fact that he was writing to the log file first gave him a false sense of security. You see, every time there was a problem with the database he'd always gone to check the log file and found the missing entries there waiting. The problem didn't become apparent until someone accidently changed the permissions on the log file and his script threw an 'access denied' error which halted execution. It never even tried to write to the database and the new entries were lost completely. So what did he do wrong?

He never added any error handling to the script. If he had simply added a "Try... Catch... Finally" block around each of the operations, the script would have achieved the desired result. Even if he didn't take the time to do anything to actually handle the errors, execution would have continued and the script would have at least tried to write the new entries to the database instead of coming to a dead stop when it ran into the permission problem.

For those of you who aren't familiar with "Try... Catch... Finally", here's a short sample script which illustrates my point. This script attempts to write to a log file and send an email. I've wrapped both operations with a "Try... Catch... Finally" block so if either (or both) fail, as they probably will if you try and run the script as is, execution will still continue to the end of the script. For illustration I've included status messages which the script will display as it runs. It'll also output the text message of any errors it encounters at the bottom of the page.

<%@ Page Language="VB" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net.Mail" %>
<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        ' Try to write to a logfile and send an email, writing status messages as we go...
        litPageProgress.Text = "<p>Page_Load sub started execution at: " & _
            Now().ToString & "</p>" & vbCrLf

        litPageProgress.Text = litPageProgress.Text & "<p>Attempt to write to log file... "
            File.AppendAllText(Server.MapPath("logfile.log"), Now())
            litPageProgress.Text = litPageProgress.Text & "Succeeded!"
        Catch exLogFile As Exception
            litErrors.Text = litErrors.Text & exLogFile.Message & "<br />"
            litPageProgress.Text = litPageProgress.Text & "Failed!"
            litPageProgress.Text = litPageProgress.Text & "</p>" & vbCrLf
        End Try

        litPageProgress.Text = litPageProgress.Text & "<p>Attempt to send email... "
            Dim mySmtpClient As New SmtpClient("localhost")
            mySmtpClient.Send("FROM_ADDRESS", "TO_ADDRESS", "Message Subject", "Message Body")

            litPageProgress.Text = litPageProgress.Text & "Succeeded!"
        Catch exLogFile As Exception
            litErrors.Text = litErrors.Text & exLogFile.Message & "<br />"
            litPageProgress.Text = litPageProgress.Text & "Failed!"
            litPageProgress.Text = litPageProgress.Text & "</p>" & vbCrLf
        End Try

        ' Note that this line will still run even if errors occur above.
        litPageProgress.Text = litPageProgress.Text & _
            "<p>Page_Load sub ended execution at: " & _
            Now().ToString & "</p>" & vbCrLf
    End Sub
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="">
<head runat="server">
    <title>Try... Catch... Finally Error Handling Example</title>
<form id="myForm" runat="server">

<asp:Literal ID="litPageProgress" runat="server" />

<br />

<p><strong>Errors Encountered:</strong></p>
<asp:Literal ID="litErrors" runat="server" />


Previous      Show All Tips      Next

If you have a tip you would like to submit, please send it to:

Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback
The Network for Technology Professionals



Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers