Home

Introduction to Exception Handling

  

Introduction

Besides the techniques that involve error handling, the Visual Basic language also supports a technique of dealing with an application's error. This is called exception handling or structured exception handling (SEH).

 

Try to Catch the Error

Errors are likely to occur on your web page(s). The more you anticipate them and take action, the better your web site will be. We have already seen that syntax errors are usually human mistakes such as misspelling, bad formulation of expressions, etc. The compiler will usually help you fix the problem by pointing it out.

SEH is based on two main keywords: Try and Catch. An exception handling section starts with the Try keyword and stops with the End Try statement. Between Try and End Try, there must by at least one Catch section. Therefore, exception handling uses the following formula:

Try

Catch
        
End Try

Exception handling always starts with the Try keyword. Under the Try line, write the normal code that the compiler must execute. Here is an example:

<script language="VB" runat="server">
Private Sub btnCalculateClick(Sender As Object, e As EventArgs)
    Dim HourlySalary As Double
    Dim WeeklySalary As Double

    Try
        HourlySalary = CDbl(txtHourlySalary.Text)

        WeeklySalary = HourlySalary * 40
        txtWeeklySalary.Text = FormatNumber(WeeklySalary)

    End Try
    
End Sub
</script>

As the compiler is treating code in the Try section, if it encounters a problem, it "gets out" of the Try section and starts looking for a Catch section. Therefore, you must always have a Catch section. If you don't, as seen on the above code, the web page will not work. A Catch section must be written before the End Try line:

<script language="VB" runat="server">
Private Sub btnCalculateClick(Sender As Object, e As EventArgs)
    Dim HourlySalary As Double
    Dim WeeklySalary As Double

    Try
        HourlySalary = CDbl(txtHourlySalary.Text)

        ' If there was an error, the flow would jump to the label
        WeeklySalary = HourlySalary * 40

        txtWeeklySalary.Text = FormatNumber(WeeklySalary)
    Catch

    End Try
    
End Sub
</script>

When the Catch keyword is simply written as above, it would be asked to treat any error that occurs. For example, here is one example of executing the above program:

Enter a number: 244.58
244.58 * 2 = 489.16

Here is another example of executing the same program:

Enter a number: 24$.58

Notice that the program stops if there is any type of problem but in this case, it doesn't bother to let the user know why there is no result displayed. Because there can be various types of errors in a program, you also should make your program more intuitive and friendlier so that, when a problem occurs, the user would know the type of problem. This is also useful if somebody calls you and says that your program is not functioning right. If there is a way the user can tell what exact type of error is displaying, may be you would find the solution faster.

 

 

 

Exceptions and Custom Messages

As mentioned already, if an error occurs when processing the program in the try section, the compiler transfer the processing to the next catch section. You can then use the catch section to deal with the error. At a minimum, you can display a message to inform the user. Here is an example:

<%@ Page Language="VB" %>
<html>
<head>

<script language="VB" runat="server">
Private Sub btnCalculateClick(Sender As Object, e As EventArgs)
    Dim HourlySalary As Double
    Dim WeeklySalary As Double

    Try
        HourlySalary = CDbl(txtHourlySalary.Text)

        ' If there was an error, the flow would jump to the label
        WeeklySalary = HourlySalary * 40

        txtWeeklySalary.Text = FormatNumber(WeeklySalary)
    Catch
	Response.Write("There was a problem with the program")
    End Try
    
End Sub
</script>

<title>Exercise</title>

</head>
<body>

<form id="frmPayrollCalculation" runat="server">
  <table>
    <tr>
      <td>Hourly Salary:</td>
      <td>
        <asp:TextBox ID="txtHourlySalary" runat="server"></asp:TextBox>
      </td>
      <td>
	<asp:Button ID="btnCalculate"
                    Text="Calculate"
                    OnClick="btnCalculateClick"
                    runat="server"></asp:Button>
      </td>
    </tr>
    <tr>
      <td>Weeekly Salary:</td>
      <td><asp:TextBox ID="txtWeeklySalary" runat="server"></asp:TextBox></td>
      <td>&nbsp;</td>
    </tr>
  </table>
</form>

</body>
</html>

Here is an example of running the web page:

Exception Handling

Of course, this type of message is not particularly clear but this time, the web page will not display a nasty message.

 
 
   
 

Home Copyright © 2005-2012 FunctionX, Inc.