Internet Explorer 5.0 Review, Part IV: Exception Handling, Comparison with VBScript- www.docjavascript.com | WebReference

Internet Explorer 5.0 Review, Part IV: Exception Handling, Comparison with VBScript- www.docjavascript.com


Internet Explorer 5.0 Review, Part IV: Exception Handling (6)

Comparison with VBScript

VBScript's support for exception handling is inferior to JavaScript's new exception handling. Up till now, if you stuck with VBScript just to enjoy its error handling, you no longer have any good reasons to stay with VBSscirpt, as JavaScript now handles exceptions. The principle behind exception handling in VBScript is very simple. You call your function and then check if there were any errors.

While in JavaScript you catch exceptions with the catch statement, in VBScript you catch them via the On Error statement. Omitting the On Error statement propagates any exception to the browser, which will display an error dialog to the user. Actually, this behavior is displayed by JavaScript as well. If you don't use the try...catch statement, any exception will be handled by the browser which will display an error message to the user. As explained earlier, the user will not have much idea where to proceed, and therefore it is important for you to handle the exceptions before the browser does. Your Web site will rank higher if you handle exceptions for your users.

The On Error is straightforward:

On Error Resume Next 

This statement just turns on the error handling. In the case of an exception anywhere in the following lines, execution will continue from the line immediately following the offending line, i.e. the line in which you call the function that causes the exception. The equivalent in JavaScript is the classic try...catch statement:

try {
  // Exceptions in this block will be handled by the following catch block.
  // Any exception will quit this block and switch control to the catch block
}
catch (exceptionObj) {
  // Handle exception from the try block above
  // Code in this block will be executed before the line immediately 
  //             following the culprit line that caused the exception
}

The major difference in catching exceptions between VBScript and JavaScript is that, once you turned on error handling, you cannot turn it back off in VBScript. Error handling in VBScript is turned off only at the end of the function. In JavaScript, on the other hand, you enclose the block of statements you want to turn on the error handling for with the try block. In short, you can control the start of the block in both VBScript and JavaScript. You can control the end of the block only in JavaScript. The end of the block in VBScript is the end of the function.

Handling exceptions in VBScript is much more complicated than catching them. In general, you need to explicitly check for error after every suspicious statement:

` turn on error handling
On Error Resume Next
` clear the Err object
Err.Clear
` The following statement or procedure call might cause an exception
Kuku()
` Check if an exception was raised
If Err.Number >  0 then
  ` handle the exception here
End If

You have noticed the Err object. Exception handling in VBScript is based on it. It has six properties which are set by the recent exception: Number, Description, Source, HelpFile, HelpContext, LastDLLError. The JavaScript's Error object shares two of these properties:

JavaScript's Error Object's PropertiesVBScript's Err Object's PropertiesProperty Description
Error.numberErr.NumberRun-time error code. Determined by the script engine or returned from an Automation object as an error code
Error.descriptionErr.DescriptionA textual description of the error

Besides its six properties, the Err object in VBScript has two methods, Clear and Raise. The need for Clear arises from the fact that VBScript does not clear the Err properties after handling an exception. In fact, there is no way for VBScript to detect when the handling of the most recent exception started or ended. You can handle an exception and then check for a new exception even though no new exception is raised. In JavaScript, there is no need for the Clear method as the catch block will be entered only after a new exception has occurred.

The Raise method is similar to the throw method in JavaScript. In a previous page we show the following JavaScript example:

<HTML>
<HEAD>
<TITLE> example 1 </TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript1.2">
<!--
function createException(msgNum, msgText) {
  this.messageNumber = msgNum;
  this.messageText = msgText;
}
function triggerException() {
  exceptionObj = new createException(1, "error blablabla");
  throw exceptionObj;
}
function raiseException() {
  try {
    triggerException();
  }
  catch (exceptionObj) {
    if (exceptionObj instanceof createException) {
      alert("Call the programmer ASAP")
    }
    else {
      throw exceptionObj;
    }
  }
}
raiseException();
// -->
</SCRIPT>
</BODY>
</HTML>

The equivalent code in VBScript would look like this:

Sub TriggerError()
  Err.Description = "error blablabla"
  Err.Number = 1
  Err.Source = "DocJavaScript Column 38"
  Err.Raise
End Sub
Sub RaiseError()
  On Error Resume Next
  Err.Clear
  TriggerError
  If Err.Number > 0 And Err.Souce == "DocJavaScript Column 38" Then 
     window.alert("Call the programmer ASAP")
End Sub

Notice that in JavaScript you cannot ask what the source of the exception was as in VBScript. Instead, you can create your own object as we did above, and you can use the instanceof check to verify which object has been caught in the catch block.


https://www.internet.com

Produced by Yehuda Shiran and Tomer Shiran

Created: April 26, 1999
Revised: April 26, 1999

URL: https://www.webreference.com/js/column38/vbscript.html