Search 
DailyCoding > Scripting

JavaScript Exception Handling Techniques

Various techniques of JavaScript exception handling are dicussed here
Author admin on Aug 4, 2009 7 Comments
Rate it    (Rated 4 by 34 people)
2,829 Views

Like the Object oriented programming the exception handling is also not used while coding in JavaScript. That why in most of cases if there is any problem in one part in a page then surprisingly other part also stops working. In this post we will be discussing the various techniques to handle exceptions in JavaScript.

Using try..catch block

try..catch block in JavaScript is very much similar to the regular C# try..catch block. The suspected code will be put in try block and all exceptions which will occur in the try block will be caught in catch block.

window.onload = function()
{
    try
    {
        var x = 90;
        var value = x / y;
    }
    catch(err)
    {
        document.write(err.name + ": " + err.message + "<br/>");
    }
}
Output:
TypeError: 'y' is undefined

In catch you will get the object containing type and description of the exception. More over you can also use finally block in the same way as you use in C#.

window.onload = function()
{
    try
    {
        var x = 90;
        var value = x / y;
    }
    catch(err)
    {
        document.write(err.name + ": " + err.message + "<br/>");
    }
    finally
    {
        alert('This is finally block');
    }
}

Using onerror event

onerror event will be raised each time there is any error while performing a action in the document. This like on place exception handling similar to Application_Error in ASP.NET. Here is sample code which demonstrate this:

window.onload = function()
{
    var x = 90;
    var value = x / y;
}

window.onerror = function(errorMeaage, fileName, lineNumber)
{
    document.write('Error: ' + errorMeaage);
}

Using jQuery Solution

It is similar to using onerror but with jQuery syntax. The syntax is:

$(window).error(
    function(errorMeaage, fileName, lineNumber)
    {
        // handle error here
    }
);
Javascript | jQuery

Discussion

very cool & good tip, thank you very On Aug 5, 2009 12:54 AM
very cool & good tip, thank you very much for sharing.

Can I share this snippet on my <a href="http://javascriptbank.com/">JavaScript library</a>?


Awaiting your response. Thank

Aaron White On Aug 10, 2009 01:39 PM
Nice post on error catching. I'm building a service that lets people instrument their website to catch Javascript errors (development & production), logging them to a hosted server (Issue management UI, etc). I'm trying to collect early alpha feedback.. it's 1 line of JS code, would be great if you wanted to give it a whirl; Rerrorize.com

Jakub Wójciak On Aug 14, 2009 02:08 AM
The window.onerror does not catch all exceptions. Firefox 2 didn't invoke it when an exception occured in an event handler. I don't know whether it's fixed by now.

I wrap the event attaching code in my own pass-through handler that catches all exceptions raised to avoid being bitten by the aforementioned bug.

Jack On Sep 6, 2009 10:34 PM
great blog, follow you!

Tarek On Nov 24, 2009 06:36 AM
Hi there I just want to know the way you make this window for coding. so it looks like .net framework

Visual C# Kicks On Jan 27, 2010 01:17 PM
Javascript exceptions are a pain in general

Gabriel McAdams On Feb 2, 2010 12:28 PM
I have just released code to help log JavaScript errors by sending error information to the server -
http://www.thecodepa....r-Notifications.aspx

Leave a Comment

Name
Email Address
Web Site
© Copyright 2008 Daily Coding • All rights reserved