Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch exceptions in jQuery

I have the following code:

<script type="text/javascript">
    $(document).ready(function() {

        $("#Save").click(function() {

            $.post("url", {
                "data": "data"
            }, function(data) {
                alert(data);
            });

        });

    });
</script>

I'm testing this script, and one of the tests I'm making is, I just close the asp.net web development server, and click the button.

IE says "access denied" error, I want to catch any error that occurs here, and display a friendly message to the user in this case.

I trying to use try/catch but didn't work...

Any clue?

like image 764
Bruno Avatar asked Jan 26 '09 01:01

Bruno


People also ask

What are JavaScript exceptions?

What is exception handling. Exception handling is one of the powerful JavaScript features to handle errors and maintain a regular JavaScript code/program flow. An exception is an object with an explanation of what went amiss. Also, it discovers where the problem occurred.

Does try-catch stop execution?

The “try… First, the code in try {...} is executed. If there were no errors, then catch (err) is ignored: the execution reaches the end of try and goes on, skipping catch . If an error occurs, then the try execution is stopped, and control flows to the beginning of catch (err) .

What is try-catch in JavaScript?

JavaScript try and catch The try statement allows you to define a block of code to be tested for errors while it is being executed. The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.

How do you handle exceptions in JavaScript?

The thrown exception is handled by wrapping the code into the try… catch block. If an error is present, the catch block will execute, else only the try block statements will get executed. Thus, in a programming language, there can be different types of errors which may disturb the proper execution of the program.


2 Answers

Use the $.ajax() method instead. It has a hook for errors.

For example:

$.ajax({
    url: 'document.xml',
    type: 'GET',
    dataType: 'xml',
    timeout: 1000,
    error: function(){
        alert('Error loading XML document');
    },
    success: function(xml){
        // do something with xml
    }
});
like image 200
cletus Avatar answered Oct 13 '22 11:10

cletus


This is a useful snippet for tracking down any jquery ajax errors in conjunction with FireBug.

// Displays any ajax errors in the Firebug console instead of hiding them
$(document).ajaxError(function(){
    if (window.console && window.console.error) {
        console.error(arguments);
    }
});

If you use Ajax with jQuery, you may have noticed that you don't get any error messages when things go wrong. Even if you have major bugs in your callback functions, jQuery just silently fails, sweeping any errors under the rug, and leaving you clueless as to what just happened.

After running this code, you'll start getting error messages in your Firebug console (if anything breaks with your Ajax calls or callbacks). The error messages aren't the greatest, but at least you don't have to stay in the dark any longer.

Credit goes to Jesse Skinner

like image 30
Ryan McGeary Avatar answered Oct 13 '22 09:10

Ryan McGeary