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

What are the ways to catch exceptions in javascript

青灯夜游
Release: 2023-01-07 11:44:16
Original
8176 people have browsed it

Method: 1. Use try catch finally statement, syntax "try{//Debug code block}catch(e) {//Capture and handle exception}finally{//Later cleanup code block}"; 2. Use the onerror event of the window object window.

What are the ways to catch exceptions in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Javascript method of catching exceptions

Exceptions in JavaScript mainly use the try catch finally statement and the window object The onerror event of window is captured.

try catch finally

try catch finallyCan only catch runtime errors, not syntax errors, you can Get the error information, stack, error file, line number, and column number. The try catch finally statement marks the block of statements to be tried and specifies a response to be thrown when an exception occurs.

try{
    // try_statements
    throw new TypeError("Test");
}catch (e){
    // catch_statements
    console.log("catch_statements");
    if(e instanceof TypeError){
        // handle this expected error
        console.log("handle this expected error");
    }else{
        // handle unexpected error
        console.log("handle unexpected error");
    }
}finally{
    // finally_statements
    console.log("finally_statements");
}

/*
    注:
    [catch (e if e instanceof TypeError) { // 非标准
       catch_statements
    }]
*/
Copy after login

You can create an error object through the constructor of Error. When a runtime error occurs, the instance object of Error will be thrown, Error Objects can also be used as base objects for user-defined exceptions. Js has several built-in standard error types:

  • EvalError: Create an error instance to indicate the cause of the error: related to eval().
  • RangeError: Create an error instance to indicate the cause of the error: the numerical variable or parameter exceeds its valid range.
  • ReferenceError: Create an error instance to indicate the cause of the error: invalid reference.
  • SyntaxError: Create an error instance to indicate the cause of the error: eval()A syntax error that occurred during the process of parsing the code.
  • TypeError: Creates an error instance to indicate the cause of the error: the variable or parameter does not belong to a valid type.
  • URIError: Create an error instance to indicate the cause of the error: give encodeURI() or decodeURl() The parameter passed is invalid.

window.onerror

window.onerrorCan capture syntax errors and runtime errors , you can get the error information, stack, error file, line number, and column number. As long as the Js script is executed in the current window, errors will be captured through window .onerror can implement front-end error monitoring. For security reasons, when a syntax error occurs in a script loaded from a different domain, the details of the syntax error will not be reported.

/*
    message:错误信息(字符串)。
    source:发生错误的脚本URL(字符串)
    lineno:发生错误的行号(数字)
    colno:发生错误的列号(数字)
    error:Error对象(对象)
    若该函数返回true,则阻止执行默认事件处理函数。
*/
window.onerror = function(message, source, lineno, colno, error) { 
    // onerror_statements
}

/*
    ErrorEvent类型的event包含有关事件和错误的所有信息。
*/
window.addEventListener('error', function(event) { 
    // onerror_statements
})
Copy after login

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of What are the ways to catch exceptions in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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