Home > Web Front-end > JS Tutorial > body text

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

云罗郡主
Release: 2018-10-22 15:39:41
forward
3107 people have browsed it

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
    }
}
Copy after login

(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);   
    }                                  
}
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template