spacer

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

home / experts / javascript / column118


JScript .NET, Part XII: Exception Handling

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


Throwing String Exceptions II

Let's look at another example that illustrates the multiple nesting levels of the try...catch...finally statements. Here is the code:

function TryCatchDemo(x){
   try {
      try {
      if (x == null)                        
         throw "x equals null";            
      else
         throw "x does not equal null";   
      }
      catch(e) {                          
         if (e == "x equals null")          
            return(e + " and handled in inner catch block.");
         else                                
            throw e;                         
      }                                      
   }
   catch(e) {                               
      return(e + ", error handled in outer catch block."); 
   }
}
print(TryCatchDemo(null)+ "\n");
print(TryCatchDemo(new Array(5)));

Notice that the finally statements are omitted, as they are only optional. We define the function TryCatchDemo() to act differently if its argument is null or not. If the argument is null, the error is handled in the inner catch block. If the argument is not null, the exception is handled in the outer catch block. We call the function twice: once with the null value, and once with a real Array object:

print(TryCatchDemo(null)+ "\n");
print(TryCatchDemo(new Array(5)));

Save this code in a file, say throw2.js, compile it, and run it. Your Command Prompt window should look like this:


Next: How to throw object exceptions

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


Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: September 9, 2002
Revised: September 9, 2002

URL: http://www.webreference.com/js/column118/5.html