Friday, January 16, 2009

Error handling in JavaScript

One way to handle error in JavaScript is to use simple try-catch block.



function foo(flag)
{
try
{
//Your code
}
catch(exception)
{
//Log exception in error log using exception object
}
}



In the above approach we have to write try-catch in all required functions.

Second approach is to register a function on window object which will get called always. It is similar to Page_Error function implementation in ASP.Net.



//Register handleError function to handles all browser error messages
window.onerror = handleError;

//Function to handle all errors.
function handleError(exception)
{
//Perform log operation
return true;
}



References :
JavaScript Error Handling

Error Handling

Error handling in javascript

No comments:

Google