Home arrow JavaScript arrow Page 4 - 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 - A basic example: handling exceptions through a generic mechanism
(Page 4 of 4 )

In order to illustrate how exceptions can be easily handled in JavaScript, let’s retake the example previously written. First off, here is a listing for the sample “loanCalculator()” function:

function loanCalculator(loanAmount,loanTerm){

    if(!loanAmount || parseFloat(loanAmount)<10000 || parseFloat(loanAmount)>300000){

        // throw exception

        throw 'Loan amount must be between $10000 and $100000';

    }

    if(!loanTerm || parseInt(loanTerm)<1 || parseInt(loanTerm)>30){

        // throw exception

        throw 'Loan term must be an integer positive value between 1 and 30';

    }

    return parseFloat(loanAmount) / parseInt(loanTerm);

}

Now that the function has been defined, let’s wrap up the calling code into a “try-catch” block, like this:

try{

    var montlyAssesment=loanCalculator(10000,45);

    if(montlyAssesment>300){

        // sorry I can't afford it

    }

    else {

        // that's a bit more affordable

    }

}

// catch thrown exceptions

catch(e){

    if(e=='Loan amount must be between $10000 and $100000'){

        // do something to process this error

        alert(e);

    }

    else if (e=='Loan term must be an integer positive value between 1 and 30'){

        // do something else to process this error

        alert(e);

    }

}

As you can see, I’ve purposely called the above function with an invalid argument (loanTerm=45), causing the function to throw an exception within the “try” block, which is trapped by the “catch” statement by using a pair of “if-else if” blocks. In this case, I’ve opted to display the exception itself, but if you find alerts annoying, just modify the code to write down the message directly within the document.

Notice that the “if-else” blocks contained within the “try” block are never executed because program control is always moved within the “catch” statement, as long as the exception is launched. Of course there is room to play with function arguments and watch how the code reacts, in order to get a clear idea on the transference of program flow.

By this point, I’ve provided you with all the makings of a simple JavaScript exception handler, being part of the introductory process for implementing exceptions trough efficient programming techniques.

Wrapping up

That’s all for the moment. Over this first part of the series, you’ve learned the basics of JavaScript exceptions, together with some explanations of how they can be implemented within a program for your convenience. However, this is only a mere introduction. There are numerous topics to be covered yet.

In the next part, I’ll be explaining different types of exceptions supported by JavaScript, so you can build up a fine-tuning mechanism to handle them in a more professional way. See you in the next part!


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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