Home arrow JavaScript arrow Page 2 - 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 - Exceptional code: introducing the basics of JavaScript exceptions
(Page 2 of 4 )

In order to understand how exceptions work in JavaScript, I’ll start with the basics –- a simple function. When a function is invoked, the only method implemented for passing data back to the calling code is the return statement. This condition is exemplified by the function listed below:

function loanCalculator(loanAmount,loanTerm){

    if(loanAmount<100000){

        return 'Loan Amount must be at least $100000';

    }

    if(loanTerm<1){

        return 'Loan term must be an integer positive value';

    }

    return parseFloat(loanAmount)/parseInt(loanTerm);

}

The function simply acts like a Utopian loan calculator (yes, no interest is charged), which returns the value of the monthly assessment to be paid according to both the loan amount and loan term arguments. Despite its simplicity, the example shows how to send data back to the fragment of code that called the function.

The above sample function might be called like this:

// call loanCalculator

alert(loanCalculator(100000,20));

Regardless of how calling code handles the data passed back by the function, what I’ve done is verify with a couple of checking lines whether the arguments conform at least to a minimum value, in order to avoid an ugly error triggered by a zero division operation. But, the problem with this function is that it requires additional code for checking input parameters. What if I want the function to save itself from possible illegal arguments? Sure I could rewrite the function like this:

function loanCalculator(loanAmount,loanTerm){

    if(!loanAmount || parseFloat(loanAmount)<1 || parseFloat(loanAmount)>300000){loanAmount=300000};

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

    return parseFloat(loanAmount) / parseInt(loanTerm);

}

And in turn, it’d be called as follows:

// call loanCalculator

alert(loanCalculator1(100000,25));

Again, I’ve coded the function for assigning default values when arguments are out of a given range. In both cases, the function is tasked with solving potential error conditions within its scope. While the examples may look like possible solutions for returning valid values, this method is considered a “bad habit” from a programming point of view.

It’s much simpler to code and read a function that does its business as long as it can, but delegates handling potential errors to a different fragment of code. This is possible by using the “throw” statement, which launches an exception within the function scope, but that can be trapped by a piece of code, at a different place inside the program. Take a look at the same function, this time using the “throw” statement:

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);

}

As you can see, now the above function uses the “throw” statement to launch an exception when something goes wrong. What’s more interesting about the rewritten function is that the payment assessment is still returned to the calling code, while when something “exceptional” occurs, the function itself delegates error handling to specific code, generally placed outside its scope. Program control is transferred to responsible code (if any) to handle unexpected conditions.

The concept of exceptions is easily understood with the above example. The goal of exceptions is to provide a built-in mechanism, which is triggered when something unusual happens (usually runtime errors), in order to handle conflictive situations, while keeping the rest of the code focused on normal program execution.

Such a powerful mechanism complements the pillars of the object-oriented paradigm, which dictates that any object should be only accessed through its interface. So, exceptions can be considered a part of the three big features of the object’s interface, meaning that a programmer is able to get at an object by using its properties, methods, and exceptions.

Now, by returning to the simple function you just saw, “throw” statements were used to report whenever an argument was considered invalid. As I explained before, when an exception is thrown, it should be caught by a fragment of code aimed at dealing in some way with raised errors. Hence there are “try-catch” blocks in JavaScript. Therefore, let’s take a look at them and explain the role that they play in JavaScript programs.


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