![]()
|
|
“To err is human, to debug is a step in the right direction” |
To make mistakes is a part of life. We as humans
live in an imperfect world, and are inherently flawed, so you can expect to do
some things wrong sooner or later. This article is no exception (I made 6 typing
mistakes so far within this paragraph alone, which I had to go back and fix).
While it’s an amiable goal to write absolutely perfect code each time out, we’ll
at some point run into a snag, whether in the execution of programs, due to the
code itself, or perhaps user input, or perhaps the unavailability of data.
A common problem Web developers and programmers alike have, especially when
designing and deploying Web apps is that data passing through them may not
always be in the proper format. These minor oversights can often have
catastrophic implications, as far as the Web goes...meaning that your program
fails, and is useless.
In our first installment of “Dev Dungeon” we’re going to look at how you can
easily implement an error-checking solution for your Web apps using the VBScript
statement On Error Resume
Next. We’ll see how four words can save
the life of your online program.
A client’s Web browser reads a page’s underlying code line-by-line, from top to
bottom. If it runs into anything which doesn’t “compute” (pun intended) with it,
it halts execution, the pages stops loading, script(s) cease to execute, and the
program fails. To compensate for the failure, Web browsers load an ugly error
message within the browser.
Examples of errors are:
Fortunately, VBScript provides us with a means
of trapping errors, and making them more manageable. With this useful utility,
we can force the browser to skip over bad code that it encounters, and continue
processing the rest of the page, while ignoring the faulty code, without causing
the entire page to fail. Developers use this technique as a means of optimizing
their (it also provides a more aesthetic means of presenting an error message
This won’t cure bad programming code---but will allow you to work more
effectively with your own programs.
To illustrate our objective, I wrote a simple page wherein the only code is a
script, which carries out a division function. And despite how domineering the
code may look, it’s really quite simple. I employed which tests a math
operation will fail no matter what – the fact that you can’t divide by zero.
For this example, we’re going to create an Active Server Page (an ASP) which
will perform some server-side scripting with just a codeblock written in
VBScript to carry out the math function, which is doomed for failure. We’re
going to intentionally write faulty code that will bomb on us.
|
As you can see, our script is a simple subroutine
which is later called down the page in line 21. Let’s go through the code,
line-by-line.
1 <% @ Language = VBScript %>
This line tells the browser what the default
processing language is for the page. If you’re new to scripting in ASP, this
step isn’t absolutely necessary, as the default scripting language for ASPs is
VBScript, but it’s good practice to explicitly define what language you’re
using.
2 <%
3 Sub PerformDivision
.....
16 End Sub
17 %>
In lines 2-3, we start our main scriptblock with a
VBScript delimiter “<%”,
and then say that we’re defining a subroutine, and give it a name. In lines
16-17, we mark the end of our subroutine and close off the scriptblock with “%>”.
5 X = 15
6 Y = 5
7 Z = 0
8
9 Response.Write("The result of the first operation, which is " & X & " and " &
Y & " is:<b> " & X/Y & "</b><br>")
10 Response.Write("The result of the second operation, which is " & X & " and "
& Z & " is:<b> " & X/Z & "</b>")
In lines 5-7 we name 3 variables (X, Y, and Z), and
assign them values of 15, 5, and 0, respectively. In our example, we’re first
going to divide X by Y (15 by 5), which of course will work, and then X by Z,
which will fail. In lines 9 and 10 we use the
Response.Write
method to print out a line of text which states which operation is being
performed (the first or second), and then display the result. Notice how we use
VBScript’s concatenation operator, the ampersand (“&”)
to escape between HTML tags and VBScript code. This allows our defined variables
to be inserted within the string being written.
4 On Error Resume Next
...
12 If Err.Number > 0 Then
13 Response.Write("<font face=tahoma size=3 color=#b22222>Your operation has
<b>NOT</b> been carried out!<br>You cannot divide by zero!")
14 Err.Clear
15 End If
This is the main part of the script you need to pay
attention to...and this is what will make our failure...more acceptable (if such
thing is possible). The statement “On
Error Resume Next” turns on VBScript’s
“watchdog” function and keeps an eye out for errors. With this statement we will
later down the page define a customized message should the subroutine perform an
illegal operation. On Error Resume Next on line 4 tells the browser (in
English), that “If you should run into any complications with this script, don’t
fail the whole program as you normally would, but skip it.” We put this at the
beginning of the script so that the browser will process it before anything else
in the script. We add on to this a construct using an If...Then statement in
line 12 which produces a customized error message I wrote. The statement
If Err.Number > 0
Then tells the browser that if it encounters any error while processing it
(again, line-by-line), to display the customized message I created, rather than
fail the whole program, and return a screen like so:
The
Err.Clear
statement on line 14 removes the error from the server’s memory, freeing up
resources. You could do a lot more by not removing it, but we won’t deal with
that right now.
19 <% Call PerformDivision %>
This line calls the subroutine and executes it on
the server, and presents the output within the client’s Web browser. Remember
that because we’re using a subroutine, the code will not be processed until it
is formally called. You could do the same thing automatically by just removing
lines 3 and 19, but I like to keep my code reusable and separate. This also
makes the arduous but equally necessary task of debugging your Web apps easier.
Since the page is an ASP, it relies on server-side processing and you’ll need to
have it running on a Web server to view it properly. If you have Microsoft’s
Personal Web Server installed on your machine, it will work, or
you can
download it here.
If you install the page on your Web server and then browse to it, you should see
the screen on the right. Without it, a screen on the left would be
generated. Try playing around with the parameters and developing new ways
to error-check this script, and then implement it in your own programs.
You could even test the script by changing the value of the Z variable in line 7
of the script to "1", and have the entire page load OK. This proves it
works!
|
|
|
On a more professional and macro level, Web devs
use this trick to secure the integrity of their scripts, and prevent against
entire pages from failing in the unforeseen event that data passing through is
in the wrong format, or missing altogether. For example, on the KUAM.COM
homepage, our national newsfeeds from MSNBC.COM have this error handling built
into them, so that if for some reason the database which feeds us national news
was down, the client would get a simple error message, like “National News is
not available at this time” in the blue box, instead of the whole page failing.
Have fun using this in your own projects!