Home arrow JavaScript arrow Page 3 - Exception Handling in JavaScript: Introduction to Core Concepts
JAVASCRIPT

Exception Handling in JavaScript: Introduction to Core Concepts


JavaScript 1.5 remedied an omission of earlier versions by including built-in support for advanced exception handling. Of course, just because the support is there doesn't mean that developers will enjoy error-free code! For this reason, JavaScript exceptions should be properly considered when writing applications. This article is intended to provide you with the basics about exceptions.

Author Info:
By: Alejandro Gervasio
Rating: 5 stars5 stars5 stars5 stars5 stars / 11
September 07, 2005
TABLE OF CONTENTS:
  1. · Exception Handling in JavaScript: Introduction to Core Concepts
  2. · Exceptional code: introducing the basics of JavaScript exceptions
  3. · Trapping exceptions: explaining “try-catch” blocks
  4. · A basic example: handling exceptions through a generic mechanism

print this article
SEARCH DEVARTICLES

Exception Handling in JavaScript: Introduction to Core Concepts - Trapping exceptions: explaining “try-catch” blocks
(Page 3 of 4 )

True to form, the official JavaScript syntax to create an exception is with the “throw” statement:

throw expression;

And, to handle an exception, it uses the standard combination of “try-catch” blocks:

try {

    execute this block

}

catch (error) {

    execute this block if an error has occurred

}

It should be noted that in a weakly typed language like JavaScript, an exception can be anything (from plain strings to objects), but unless a specific exception type is launched by a “throw” statement, whenever an error arises, JavaScript makes automatically available an Error object, which is created when an error is triggered.

Additionally, JavaScript supports handling of multiple exceptions through two different structures. First, by using multiple “try-catch” blocks, as follows:

try {

    execute this block

}

catch (error type1) {        

    execute this block if an error of type 1 has occurred

}

catch (error type2) {

    execute this block if an error of type 2 has occurred

}

finally {                   

    execute this block after the try block

}

And second, different types of exceptions can be trapped by using multiple “if” statements within a single “catch” block:

try {

    execute this block

}

catch (error) {

    if(error instanceOf error type1) { 

        execute this block if an error of type 1 has occurred

    }

    else if(error instanceOf error type2) { 

        execute this block if an error of type 2 has occurred

    }

    else if(error instanceOf error type3) { 

        execute this block if an error of type 3 has occurred

    }

    else {

        execute this block is none of the above errors occurred

    }

}

Although both forms of syntax are valid for dealing with multiple exceptions, the last one listed is by far more popular because of its close intuitive similarity with “if-else if” control blocks found on most programming languages.

Now, allow me to explain how the flow of a program is handled in each pertinent case. In the first case, if an exception is found when executing the code within the “try” block, the JavaScript interpreter will stop the program execution at that point and search for a “catch” block that contains the proper exception handler. After the exception has been handled or no catch blocks present a matching exception handler, then program execution is transferred to the “finally” block.

In the second case, program flow is handled in a similar way. If an exception is encountered during the execution of the “try” block, the program is stopped at that point and JavaScript will look for an exception handler that matches the type of error triggered, within the successive “if-else if” blocks. If the exception has been handled within an “if” block, (and assuming that the program hasn’t been halted), control is returned to the code following the ‘try” statement. Otherwise, if no exception handler is found on each “if” structure, the code within the final “else” block is executed.

To demonstrate how this process works, an example is preferred in all cases, so let’s jump over the next lines to see a few pieces of illustrative code.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials