Eric Baldwin
Eric Baldwin

Reputation: 3501

How can I tell if an error occurred in a function call in javascript?

I have the following code structure:

callFunction()

if (there was an error in callFunction()){
    //Execute code
}

What can I put in place of 'there was an error in callFunction()' that will tell me an error was called in that function? Is there line I should add to callFunction itself?

Upvotes: 0

Views: 36

Answers (1)

mwilson
mwilson

Reputation: 12950

try catch is one way to do it. http://www.w3schools.com/js/js_errors.asp

For example:

function myFunction() {

    try {
        //some code to execute
    }
    catch (e) {
        //throw an alert message if the try statement failed
        alert(e.toString());
    }
}

Upvotes: 1

Related Questions