spacer
Yehuda Shiran September 12, 2002
JScript .NET's Exception Handling Advantage
Tips: September 2002

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
Google Chrome Playing Catch-Up on Extensions
Open Solutions Alliance Gets New Leadership
Red Hat Spacewalk Expands Linux Management
In any programming language, your code will throw a run-time exception when trying to execute an illegal command. One of the most trivial illegal operations you may attempt is accessing a null object. Your operating system will definitely complain when trying to access a null object. Accessing an array element that is out of the array's legal range is another example.

More often than not, you want to avoid system errors. System messages are usually cryptic and do not make sense for the average user. In fact, system messages are bad for your reputation as a programmer, as they are clear and tangible evidence of your bugs. Naturally, you'll want to avoid these messages by checking for them in your code, before they hit the operating system. Checking for exceptions in your code will surely make it more cumbersome. Mingling error-checking with the normal flow of your code is inevitable when the language does not support exception handling. The following skeleton code checks for two errors: accessing a null object and out of range array element:

  var newObject = createObject(dataPiece1, dataPiece2, dataPiece3);
  if (newObject == null) {
    // do something here when the object was not created
    ...
  }
  else {
    // getElementIndex return an index or -1 for an error
    var index = newObject.getElementIndex();
    if (index == -1) {
      // handle the error case
      ...
    }
    else {
     // finally, you can do something with index
     ...
    }
  }
The crux of the exception handling support in JScript .NET (as well as in other languages, as explained in Column 38) is the try...catch...finally statement. The try keyword precedes a block of normal processing code that may throw an exception. The catch keyword precedes a block of exception handling code. The finally keyword precedes a block of code that will always be executed after handling the exceptions. Once an exception is thrown from a try block, control over the program flow switches to the first catch block following it. Here is the try...catch...finally block you may use to mimic the flow above:

  try {
    var newObject = createObject(dataPiece1, dataPiece2, dataPiece3);
    var index = newObject.getElementIndex();
      // do something with index
  }
  catch() {
    alert("System Error; Call your sw vendor");
  }
  finally {
    // do nothing
  }
To learn more about exception handling, go to Column 118, JScript .NET, Part XII: Exception Handling.


People who read this tip also read these tips:

Look for similar tips by subject:

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

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

webref The latest from WebReference.com Browse >
Administering MySQL Databases on the Web Using PHP · Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
So what is an Oracle Nested Table? · E-Discovery Architectures 101 · eBay Embraces Big Sellers, Cyber Monday Trends