Home arrow JavaScript arrow Page 4 - JavaScript Exception Handling

Being Verbose - 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



Now, what you just saw was a very primitive exception handler, albeit one which isn't very useful on a production site. Ideally, you'd want the message generated by the exception handler to be a little more descriptive, and to contain some information on the source of the error.

JavaScript satisfies this requirement via two special, pre-defined properties of the Error object generated by an exception - the "name" and "message" properties. Take a look:

<script language="JavaScript">

try {
colours[2] = "red";
} catch (e) {
alert("An exception occurred in the script. Error name: " + e.name + ". Error message: " + e.message); }


Here's the output:

An exception occurred in the script. Error name: TypeError. Error message: 'colours' is undefined

Don't want to use the alert() box because it's disruptive and annoying? Write the output directly to the page itself with the writeln() method instead, as below:

It's also possible to write an exception handler to ignore all errors generated in your script - the following example demonstrates:

<script language="JavaScript">

try {
// generate an error
eval("some gibberish")
} catch (e) {
// ignore it


In this case, it doesn't matter what type of exception JavaScript generates
- the generic handler will catch it, ignore it and continue to process the rest of the script.

It should be noted, however, that this approach, although extremely simple, is *not* recommended for general use. It is poor programming practice to trap all errors, regardless of type, and ignore them; it is far better - and more professional - to anticipate the likely errors ahead of time, and use the "try-catch" construct to isolate and resolve them.

>>> 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: