Home arrow JavaScript arrow Page 3 - JavaScript Exception Handling

Playing Catch - JavaScript

Wondering why JavaScript doesn't include exception-handling constructs like its bigger cousins? Well, the newest version of JavaScript does - and this article tells you all about it, explaining how you can use the new Error object and the "try-catch" constructs to trap and resolve errors in script execution.

  1. JavaScript Exception Handling
  2. Anatomy Of An Exception
  3. Playing Catch
  4. Being Verbose
  5. All For One...
  6. The Final Solution
  7. Raising The Bar
  8. Endzone
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 239
August 14, 2003

print this article



So that's what an error looks like. And now that you've seen what makes it tick, how about writing something to handle it?

JavaScript allows you to trap errors using the standard "try-catch" exception-handling combination. Here's what it looks like:

try {
execute this block
} catch (error) {
execute this block if error

Take a look at this rewrite of the previous example, which incorporates a simple exception handler.

<script language="JavaScript">

try {
colours[2] = "red";
} catch (e) {
alert("Oops! Something bad just happened. Calling 911...");


And this time, when the script is executed, it will not generate an error - instead, it will output

Oops! Something bad just happened. Calling 911...

This is a very basic example of how JavaScript exceptions can be trapped, and appropriate action triggered. As you will see, the ability to handle errors in a transparent manner throws open some pretty powerful possibilities...but more on that later.

The first part of the code introduces you to the "try" block; this instructs JavaScript to try - literally - to execute the statements within the enclosing block.

try {
colours[2] = "red";

The "catch" block sets up the exception handler, in the event that one is generated. This does the hard work of trapping the exception and suggesting how to handle it

catch (e) {
alert("Oops! Something bad just happened. Calling 911...");

If an exception is generated, the "catch" segment of the code will be triggered, and JavaScript's default error handling mechanism will be overridden by the instructions in that code segment - in this case, displaying the text "Something bad happened".

>>> More JavaScript Articles          >>> More By icarus, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Javascript for Beginners: An Introduction
- Introduction to JavaScript
- Adding Elements to a Tree with TreeView jQue...
- Using the Persist Argument in a TreeView jQu...
- Using Unique and Toggle in a TreeView jQuery...
- Using Event Delegation for Mouseover Events ...
- Using the Animate Option in a Treeview jQuer...
- Using HTML Lists with Event Delegation in Ja...
- Opened and Closed Branches on a TreeView jQu...
- Mouseover Events and Event Delegation in Jav...
- Creating a TreeView JQuery Hierarchical Navi...
- Event Delegation in JavaScript
- A Look at the New YUI Carousel Control
- Working with Draggable Elements and Transpar...
- Displaying Pinned Handles with Resizable Con...

Developer Shed Affiliates


Dev Shed Tutorial Topics: