spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column38


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

Sr Instructional Designer D2L-Moodle,Clearance
WSI Nationwide, Inc.
US-NJ-Fort Monmouth

Justtechjobs.com Post A Job | Post A Resume
Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs


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.


http://www.internet.com

Produced by Yehuda Shiran and Tomer Shiran

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint


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

URL: http://www.webreference.com/js/column38/realexample.html