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

The most complete guide to JavaScript error handling

coldplay.xixi
Release: 2020-09-16 16:31:17
forward
1849 people have browsed it

The most complete guide to JavaScript error handling

Related learning recommendations: javascript video tutorial

What are programming errors

us The development process was not always smooth sailing. In particular, in some cases we may want to stop a program or notify the user if something bad happens.

For example:

  • The program tried to open a file that does not exist,
  • The network connection was disconnected
  • The user entered an invalid character

In situations like these, we can write a custom error to manage ourselves, or directly let the engine define these errors for us. With the error defined, we can notify the user with a message or stop the execution of the program.

What is an error in JavaScript

An error in JavaScript is an object. To create an error in JS, you can use the Error object, as shown below:

const err = new Error('霍霍,好像哪里出问题了!')复制代码
Copy after login

You can also omit the new keyword:

const err = Error('霍霍,好像哪里出问题了!')复制代码
Copy after login

Create , the error object has three attributes:

  • message: A string with the error message

  • name : Type of error

  • stack: Stack trace of function execution

For example, we use TypeError object creates an error, the corresponding message is the created character symbol passed in, name is "TypeError"

The above is the detailed content of The most complete guide to JavaScript error handling. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.im
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