js Error Essays-Error Tips Sanlian's Error

The most common thing in programming is Error, not Success. After all, there is no Success object in JavaScript.

The Error object will be the topic to be discussed below

Error-JavaScript | MDN
can create an error object through the Error constructor



The usage of Error is very simple, Error("error message"); new Error("error message"); can use new or not

The so-called "create an error object", I think Error() is more like providing an error format

> Error("报错信息")

Error: 报错信息
    at <anonymous>:1:1

> EvalError("报错信息")

EvalError: 报错信息
    at <anonymous>:1:1

It is the same as the error format reported by the system

> 报错信息

Uncaught ReferenceError: 报错信息 is not defined
    at html.html:49

As for how to trigger the error (throw), how to catch the error (try...catch), how to output the error (console.error()), etc., these are not the issues to be discussed in this article.



In addition to Error this Error, it also has 6 other types of children (InternalError is an illegitimate child, not in this list)

  • EvalError
  • RangeError
  • ReferenceError
  • SyntaxError
  • TypeError
  • URIError
EvalError() instanceof Error // true
RangeError() instanceof Error // true
ReferenceError() instanceof Error // true
SyntaxError() instanceof Error // true
TypeError() instanceof Error // true
URIError() instanceof Error // true

EvalError() instanceof EvalError // true
RangeError() instanceof EvalError // false

The usage of these 6 types is the same as Error. You can use instanceof to determine what type they belong to, and these 6 types belong to Error.



A complete error prompt has at least three steps

  • Error: Create an error object
  • throw: throw (trigger) an error
  • try...catch: catch (handle) errors

General usage demonstration

function fun() {
    
    
	// new Error() 创建一个错误对象
	// 用 throw 抛出一个错误
	throw new ReferenceError("这是一个报错信息")
}

try {
    
    
	// 在 try 中执行一个可以报错的函数
	fun()
} catch(error) {
    
    
	// 函数如期抛出错误, 并被 catch 捕获

	// 打印捕获的报错信息
	console.log(error)
}



Error prompt three consecutive

  1. js Error Essays-Error Tips Sanlian's Error
  2. js throw statement essay-error report and three consecutive throw
  3. js try...catch statement essay-error prompts three consecutive try...catch

end

Guess you like

Origin blog.csdn.net/u013970232/article/details/109646457