Home >Web Front-end >JS Tutorial >How to deal with errors in JavaScript? Error handling methods in JavaScript

How to deal with errors in JavaScript? Error handling methods in JavaScript

云罗郡主
云罗郡主forward
2018-10-22 15:39:213130browse

The content of this article is about how to deal with errors in JavaScript? The error handling methods in JavaScript have certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

(1) One way to handle thrown exceptions is to use try-catch directly at the top of the call stack, like:

function main(){
    try{
        bomb();
    }catch(e){
        //Handle all the error things
    }
}

(2) However, the processor is event-driven, JavaScript An error in is also an event that is released after the parser has executed in the current execution context.

We can use an onerror global exception event handling function:

if(window.addEventListener){
    window.addEventListener('error',function(e){
        var error = e.error;
       consle.log(error);
    })
}else if(window.attachEvent){
    window.attachEvent('error',function(e){
        var error = e.error;
        consle.log(error);
    }
}else{
        window.onerror = function)(){
       var error = e.error;
       consle.log(error);   
    }                                  
}

The above is how to deal with errors in JavaScript? A complete introduction to error handling methods in JavaScript. If you want to know more about JavaScript Video Tutorial, please pay attention to the PHP Chinese website.

The above is the detailed content of How to deal with errors in JavaScript? Error handling methods in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete

Related articles

See more