Javascript Scripting Tutorial Pt 5

Error Handling in Javascript

I’ve covered most everything you need to know to program in Javascript except for Error Handling. So, in this article I’ll explain how to handle those nasty errors when they crop up.

An exception is thrown whenever an error occurs in your code. You can set up your code in a way, so that it throws exceptions that you have a pre-built solution for. Let us say one of your functions divides numbers. We all know that dividing into zero produces a result of infinite. So, it would be a good idea to head off a zero division. How would you do that?

You would write some code that throws the exception or error and then catch and respond to such an error. The throw statement follows this style: throw expression;

Normally, the value of the expression is a string that represents what error caused an exception to be thrown. Ex. throw new error(“A division by zero occured”);

When an exception is thrown, the Javascript interpreter jumps to the nearest exception or error handler. Those exception handlers are defined by code that is surrounded by curly braces and labeled with the words try, catch, or finally. If there is no code defined for exception handling, then the visitor to your site is shown an error and that is a bad thing.

try, catch and finally Exception/Error Handlers

  • try clause: defines the block of code that handles an errors that occur
  • catch clause: defines the block of code that handles any errors that occur in the try clause
  • finally clause: is always executed, every time the try clause code is executed, and provides final clean up for any errors that occur

Example of error handling process:

try {

… An error is spotted by the javascript interpreter and the interpreter looks for a try clause. In the code for the try clause we do are best to resolve the error, so the visitor to our site never is alerted to the error we made…

}

catch(e) {

… Only if the try clause doesn’t resolve the error, is the error object thrown to the catch code clause. This block of code can be written to resolve the error, ignore it, or throw it again to be resolved by another piece of code…

}

finally {

… This block of code is executed every time a try clause is called. It performs final clean up for any errors that occur with your program …

}

Real World Example:

try {

if ( divisor == 0 )
{
var d = prompt(“We cannot accept the value zero. Please enter a new value:”, “”);
}}

catch (e) { // If the user entered an incorrect value we are thrown here
alert(e); // Tells the user what error occured
}

If you want to signal to the interpreter that you want to throw an error of your own simply execute the throw keyword. You would signal that an error has occurred with a division by zero with the following code:

if(d == 0) throw new Error(“You cannot divide by zero”);

If this if executed, the interpreter would jump to the try clause and try and resolve the issue.

That is basically all there is to know about error handling with Javascript Scripting. In the next and final article, I will perform some cleanup of my own, by covering many of the useful pre-built functions that Javascript provides to you. Then you will be a Javascript Expert!

Till next time…

No Responses to “Javascript Scripting Tutorial Pt 5”

Trackbacks/Pingbacks

  1. Tweets that mention Javascript Scripting Tutorial, Free Javascripts, Errors | new think tank -- Topsy.com - [...] This post was mentioned on Twitter by Derek Banas. Derek Banas said: Published: Javascript Scripting Tutorial Pt 5 @…

Leave a Reply

Your email address will not be published.

Google+