Javascript Tutorial: Handling All Errors

I found a very interesting method to handle errors on JavaScript. What this does is to handle all error occurs in the script to a single function so whenever any error occurs, it will go into this particular function and alert the user.

JavaScript

function handleError()
{
alert(An error has occurred!');
return true;
}
window.onerror = handleError;

What it does is that whenever a run time error occurs, the function will goes into action! For example you try to trigger a function that does not exist! Instead of making your program stop functioning, we can provide a error message using the above method which can promote a more user friendly environment whenever an error occurs on our script. This is neat stuff!

 

Like this post? Share it!

digg 48 Javascript Tutorial: Handling All Errors reddit 48 Javascript Tutorial: Handling All Errors stumbleupon 48 Javascript Tutorial: Handling All Errors delicious 48 Javascript Tutorial: Handling All Errors furl 48 Javascript Tutorial: Handling All Errors technorati 48 Javascript Tutorial: Handling All Errors google 48 Javascript Tutorial: Handling All Errors myspace 48 Javascript Tutorial: Handling All Errors facebook 48 Javascript Tutorial: Handling All Errors twitter 48 Javascript Tutorial: Handling All Errors
share save 171 16 Javascript Tutorial: Handling All Errors

No related posts.

About Clay

I am Clay who is the main writer for this website. I own a small web hosting company in Malaysia and i'm available to be hired as individual contractor on elance. You can find me on twitter.
This entry was posted in How-to, JavaScript and tagged . Bookmark the permalink.

One Response to Javascript Tutorial: Handling All Errors

  1. mayo says:

    nice trick, thank you ! a minor change to report the error description :

    function handleError(e)
    {
    alert(‘An error has occurred!\n’+e);
    return true;
    }
    window.onerror = handleError;