如何判断javascript函数调用中是否发生错误?

时间:2014-10-25 22:16:31

标签: javascript

我有以下代码结构:

callFunction()

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

我可以用什么代替'callFunction()中的错误,它会告诉我在该函数中调用了错误?我应该添加到callFunction本身的行吗?

1 个答案:

答案 0 :(得分:1)

try catch是一种方法。 http://www.w3schools.com/js/js_errors.asp

例如:

function myFunction() {

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