spacer
Yehuda Shiran September 17, 2002
Nesting the try...catch Statement
Tips: September 2002

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
OpenOffice 3.2 Lands Amid Critical Changes
Red Hat, IBM Firmly in KVM Virtualization Camp
Red Hat Talks Up Open Source Cloud Plans

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.

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:


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Search Engine Optimization: Selecting and Embedding Keywords · Are Google's Language Translation Web Services Ready for Prime Time? · Installing and Using Meeplace, the Business Review CMS
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
IBM DB2 10 for z/OS: Justifying the Upgrade · Living La Vida Colo: Choosing the Right Colocation Facility · FTC Concerns over Social Media Privacy Linger