Chapter 4. Error Handling and Debugging
Errors, bugs, and therefore debugging are a part of life for a programmer. As the saying goes, if you haven’t made any mistakes, then you aren’t trying hard enough.
Dealing with errors actually involves two very different processes: error handling and debugging. Error handling is a combination of coding and methodology that allows your program to anticipate user and other errors. It allows you to create a robust program. Error handling does not involve weeding out bugs and glitches in your source code, although some of the error handling techniques covered in this chapter can be used to great advantage at the debugging stage. In general, error handling should be part of your overall program plan, so that when you have an error-free script, nothing is going to bring it to a screeching halt. With some sturdy error handling in place, your program should be able to keep running despite all the misuse that your users can—and certainly will—throw at it.
The following ASP page illustrates some simple error handling:
<HTML>
<HEAD><TITLE>Error Checking</TITLE>
<BODY>
<SCRIPT LANGUAGE=VBSCRIPT RUNAT=SERVER>
Dim n, x
n = 10
x = Request.Form.Item("txtNumber")
If x = 0 Or Not IsNumeric(x) Then
Response.Write "x is an invalid entry"
Else
y = n / x
Response.Write y
End If
</SCRIPT>
</BODY>
</HTML>The error handling in this example is the best kind—it stops
an error before it can occur. Suppose you hadn’t used the
conditional If...Else statement and had allowed ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access