js throw statement essay-error report and three consecutive throw

I believe that many coders are not familiar with throw statements, because the first goal of coders is to eliminate rewards, and the main function of throw is to report rewards.

In user-oriented projects, program error reporting is probably a very bad user experience,
but things have two sides. In developer-oriented framework tools, error reporting is undoubtedly an indispensable important content.



throw-JavaScript | MDN
throw statement is used to throw a user-defined exception. The execution of the current function will be stopped (the statement after throw will not be executed), and control will be passed to the first catch block in the call stack. If there is no catch block in the caller function, the program will terminate.



Throw can only be used in try...catch. Once triggered, the following code will be blocked, and the error will be caught by catch
. The usage of throw is very similar to the usage of return. The throw "error message"
error message is not explicitly required, that is Say it can be new Error(), string, array, object, etc.

throw {
    
     a: 1 }
throw "报错信息"
throw [1, 2, 3]
throw true

If throw is not caught, it will report directly to the console and block future code



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/109648025