Exception Handling In Javascript

There are various ways to handle exception in JavaScript.

1. try..catch..finally block

function performDivision(x,y)
{
    try {
        var z = x/y;
        return z;
    }
    catch(ex) {
        //raise error e


x.message
    }
    finally {
        //do something
    }

}

2. Browser’s onerror event

window.onerror = function(exMessage, name, lnNo)
{
    //raise error with exMessage
}

But what you would do with these error. One should send all these error back to server and log them.

To do so, on any JavaScript error raised, send the error to server and log it.

function raiseError(exStr)
{
     var xmlHttpObj = getXmlHttpObject();
     xmlHttpObj .open('POST',url,true)
     xmlHttpObj.send(exStr);
}
Tags: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*