Home  >  Article  >  Web Front-end  >  The most complete guide to JavaScript error handling

The most complete guide to JavaScript error handling

coldplay.xixi
coldplay.xixiforward
2020-09-16 16:31:171625browse

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('霍霍,好像哪里出问题了!')复制代码

You can also omit the new keyword:

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

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!

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