Internet Explorer 5.0 Review, Part IV: Exception Handling, A Real Example - www.docjavascript.com | WebReference

Internet Explorer 5.0 Review, Part IV: Exception Handling, A Real Example - www.docjavascript.com


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

Programmer-thrown Exceptions - A Real Example

Let's use now the exception handling functions from the previous page to handle a more realistic example. Suppose you have a page with one <IMG> tag. The program accepts a few attribute names and extracts the value of the corresponding image object properties. The possible exceptions caused by accessing incorrect attribute names and out-of-bound array elements. Here is the complete code:

<HTML>
<HEAD>
<TITLE> example 1 </TITLE>
</HEAD>
<BODY>
<IMG SRC="doc.gif">
<SCRIPT LANGUAGE="JavaScript1.2">
<!--
function createException(msgNum, msgText) {
  this.messageNumber = msgNum;
  this.messageText = msgText;
}
function triggerException(messageNum, messageTxt) {
  exceptionObj = new createException(messageNum, messageTxt);
  throw exceptionObj;
}
function raiseException(num, message) {
  try {
    triggerException(num, message);
  }
  catch (exceptionObj) {
    if (exceptionObj instanceof createException) {
      alert("Call the programmer ASAP")
    }
    else {
      throw exceptionObj;
    }
  }
}
fieldNames = new Array("border", "complete", "height", "hspace",
  "lowsrc", "name", "src", "vspace", "width");
  // (The above two lines should be joined as one line.
  // They have been split for formatting purposes.)
try {
  for (image=0; image < document.images.length + 1; image++) {
    for (i=0; i < fieldNames.length; i++) {
      if (document.images[image] == null) triggerException( 98,
        "illegal access to images array");
  // (The above two lines should be joined as one line.
  // They have been split for formatting purposes.)
      if (eval("document.images[image]." + fieldNames[i]) != null)
        alert(fieldNames[i] + " is " + eval("document.images[image]."
        + fieldNames[i]))
  // (The above three lines should be joined as one line.
  // They have been split for formatting purposes.)
      else triggerException(i, "field name " + fieldNames[i]  + "
       is not legal");
  // (The above two lines should be joined as one line.
  // They have been split for formatting purposes.)
    }
  }
}
catch (exceptionObj) {
  if (exceptionObj instanceof createException) {
    if (exceptionObj.messageNumber > 0 && exceptionObj.messageNumber
      < 9) alert("Problem in property: " + exceptionObj.messageText)
  // (The above two lines should be joined as one line.
  // They have been split for formatting purposes.)
    else if (exceptionObj.messageNumber < = 98 &&
      exceptionObj.messageNumber  >= 97) alert("Problem in images array: "
      + exceptionObj.messageText)
  // (The above three lines should be joined as one line.
  // They have been split for formatting purposes.)
           else raiseException(99, "Catch All");;
  }
}
//raiseException(1, "Doc JavaScript Column 36");
// -->
</SCRIPT>
</BODY>
</HTML>

We first define the fieldNames array. The try block consists of two nested loops. The outer one iterates through the page's images:

for (image=0; image < document.images.length + 1; image++) {

The inner loop iterates through the fieldNames elements:

for (i=0; i < fieldNames.length; i++) {

Inside the loop, we check for two exceptions. The first check verifies that there is no attempt to access an out-of-bound array element. We trigger an exception when there is an invalid attempt:

if (document.images[image] == null) triggerException( 98, "illegal access to images array");

The second check prints the value of the relevant object's property or triggers an exception when the field name does not match any of the object's:


if (eval("document.images[image]." + fieldNames[i]) != null) alert(fieldNames[i] +
  " is " + eval("document.images[image]." + fieldNames[i]))
  // (The above two lines should be joined as one line.
  // They have been split for formatting purposes.)
      else triggerException(i, "field name " + fieldNames[i]  + " is not legal");

The catch block decipher the exception objects. First, we check if the object is of the right type:

if (exceptionObj instanceof createException) {

As explained earlier, this check is of utmost importance for distinguishing between JavaScript errors and user errors. Once we know it is a user-thrown exception, we check if the error number is between 1 and 9, and print an appropriate message.


if (exceptionObj.messageNumber > 0 && exceptionObj.messageNumber < 9) alert("Problem
  in property: " + exceptionObj.messageText)
  // (The above two lines should be joined as one line.
  // They have been split for formatting purposes.)

We check if the error number is 98 or 97 and then print a message related to the exception:


else if (exceptionObj.messageNumber < = 98 && exceptionObj.messageNumber  >= 97)
  alert("Problem in images array: " + exceptionObj.messageText) 
  // (The above two lines should be joined as one line.
  // They have been split for formatting purposes.)

If the exception object does not match any of the error types, we raise a general exception to be handled by the operating system:

else raiseException(99, "Catch All")

Always remember that if you don't handle the exception, someone else will. The operation system handles all unattended exceptions.


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/realexample.html