You are on page 1of 23

Envision , Evolve, Unleash

VB Script

Error Handing and Debugging by


Srinivas Malkapeta & Prasanna
Agenda

• Error Handling
Error Handing is the programmer’s line of defense against this
inherent unpredictability.The term error handing refers not only to
how a program responds when an error occurs, but also to how it
prevents errors from happening in the first place.

• Debugging
Debugging, as the name suggests, is the process of detecting,
locating, and removing bugs from a program.
Types of Errors

• Syntax Errors: Syntax errors will halt the execution of the


script.

• Runtime Errors: Runtime errors will invoke an error


handler, giving you the opportunity to do something about the error-
or at least display it attractively.

• Logic Errors: Logical errors will most commonly


contaminated data in your application, cause strange behavior for
users and administrators, and often cause other errors to occur.
Syntax Errors

Before the script is run, the scripting engine parsers all of the
code,converting it into tokens.When an unrecognizable structure or
an expression is encountered, a syntax error is generated.

Syntax errors tend to popup in several circumstances:


• When something is missing from the code-parentheses, keywords
(especially in blocks), statement elements, or when the keyword are
simply out of place.
• When a keyword is misspelled or used incorrectly
• When you try to use a Visual Basic or VBA keyword or function that
is not implemented by VBScript
• When you use keywords that are not supported by a particular
version of the scripting engine (certain keywords may be phased
out, and others added)
Runtime Errors

• This is the most common type of error is the runtime error. A


runtime error occurs when a command attempts to perform an
action that is invalid.
• In that the offending code looks syntactically fine to the script
engine. Some kind of problem when it is executed. That is, the
error does not necessarily appear while you are programming,
but rather when you or one of your users is running the script.
• It divided into three categories:
» Native VBScript runtime errors.
» Non-VBScript runtime errors.
» Variable declaration runtime errors related to the Option Explicit
directive.
• Result is same in 3 cases: an error occurs while the script is
running. What differs the 3 types is:
» What causes an error.
» How an error could be prevented.
Runtime Errors

• Native VBScript runtime errors:


This raises an error where something that computers don’t like;
dividing by zero
• Non-VBScript runtime errors”:
Runtime errors will often come from other sources (scripting hosts, MS-
ADO comp. any script can use err. raise method.
The method of dealing with non-VBScript runtime errors is the same as
Native VBScript runtime errors. Ideally the error msg itself will give you enough
information to make the cause of the error. Ex:.For MS-ADO specified table is
not existed.

• Variable declaration runtime errors related to the Option


Explicit directive
For VBScript Prgmer, it is difficult to detect errors that can result from
misspellings of variable names and mistakes related to a variable’s scope.
Logical errors

• You can think of logic errors as hidden errors. In general it will not
produce any kind of err msg. Instead, a logic error will produce what
programmers usually call “undesirable results”.
For ex: Sales tax calculations. Miscalculated sales tax can even
cause serious legal and financial problems.
• We can prevent some logical errors by clear cut of requirements and
design specifications, and some more logical errors can still creep
into code. Make use of “plus” operator instead of “minus” or some
other operator which is actually have to use.
• Another preventative measure against logic errors is what is known as
defensive programming. It is about checking assumptions about the
expected program flow and either
1. generating runtime errors 2. Including extra code
• Particularly complex problems in the form of procedures, functions,
classes and so on.
Error Visibility and Context

A key aspect of understanding and managing runtime


errors in knowledge of where your code is running
and what happens when an error occurs. Later we
will discuss “error handling” techniques, which can
be used to catch errors when they occur and control
what happens after that point.

• Windows Script Host Errors


• Server-Side ASP Errors
• Client-Side VBScript Errors in Internet Explorer
WSH(Windows Script Host Errors)

• WSH has an automatic error display mechanism. With human


operator sitting infront of the computer. If an error occurs, WSH
displays it in a dialog box and the human operator decide what to do
at that point.
without a human operator, you can control to some degree
what happens when an error occurs instead of using WSH’s default
error display mechanism. The section Handling errors with various
techniques.
Server-Side ASP Errors
A server-side ASP error can occur when IIS is processing ASP
code.
Client-Side VBScript Errors in Internet Explorer:
Handing Errors

Approach to responding errors.


» Displaying the error to a user.
» Logging the error to a file, database, or the WEL
» E-mailing the error to SA.
» Paging the SA
» Some combination of all this
• The Err Object
• Using the On Error Statements
• Presenting and Logging Errors
• Server-Side ASP Errors
The Err Object

It is an “intrinsic object with global scope. Always with vb-


script code no need to declare a variable to hold an Err object
and no need to instantiate it using CreateObject or New.
Exactly one Err obj in memory all time .
The Error object contains information about the last error
that occurred. The object has two methods.
3. Clear method . ( the last error is thrown away)

Err Object is additionally cleared by any of the following


On error resume next, On error goto 0,Exit sub, Exit function

2. Raise method. ( which you can use in your VBScript code to


generate custom runtime errors. Some main properties to
display the information of error are:

Description, Number, Source, HelpFile, HelpContext


Using the On error Statements

• Usually by default WSH displays error for script. Checking errors at


runtime switch is On every time by using on error resume next
switch is off ,if any error that occurs will essentially be ignored.
Means it is globally supresses errors for your entire script including
procedures, functions, classes. But we can propose the following
two rules.
• Unless you have a very good reason, never suppress all
errors by simply placing the On Error Resume next
statement at the top of your script.
• On error Resume Next statement to temporarily disable
error reporting, make sure you use a corresponding On
Error GoTo 0 statement to enable it again.
(If you do not follow the above statement then, without realizing
it, suppress error later in your script.
Logging Errors

• Beyond displaying the error, you have other options as well. In fact,
if your script runs unattended, you might not want to display the
error at all since there is no one sitting there to click the OK button.
One of the most popular techniques is to log the error to a file, a
database, or the Windows Event Log, or e-mail to sys admin, or
page to the sys admin.
Generating Custom Errors

• Provides ability to “raise” errors yourself. Any vbscript code


you write can at any time stop execution and generate an error.
The key to this ability is the raise method of the Err object

• Using Err.Raise
• When Not to Use Err.Raise
• When to Generate Custom Errors
Using Err.Raise

• It is always available for global scope. That means you can


refer to it any time you want without having to declare a
variable for it.
Syntax is:
Err. Raise (number, source, description, helpfile, helpcontext)

Ex: Err. raise vbObjectError+ 10000,_ “ MyScript.myFuntion”,” the


Age arg of Myfunc may not be greater than 150.”

Error number between 0 and 65535, 0 as the absence of an error.

Adding vbobjecterror to your error num makes sure your error


num will not clash with “official microsoft error numbers.
When to Generate and Not to Custom Error

Not to Generate
Ex: err.raise vbObjectError+15000, “ERR_MSG_UGLY.VBS”,
“Hey, stupid, you can’t enter a zero! It will “ & “cause a divide
by zero error!”

To Generate
Ex: err.raise vbObjectError+15000,
“ERR_MSG_NICE.Dividenumbers()”, “Divison by zero not
allowed”
Debugging

What is Debugging?
Debugging

Options for Debugging VBScript Code


Debugging

Debugging without a Debugger


Debugging

Debugging WSH Scripts with the Microsoft


Script Debugger
Debugging

Debugging Client-Side Web Scripts with the


Microsoft Script Debugger
Debugging

Debugging ASP with the Microsoft Script


Debugger
Debugging

Using the Microsoft Script Debugger

You might also like