preface
I’m going to split
And “Front-end Magic: Call Stack, Treasure of Exception Instances” describe built-in/custom exception classes, capturing runtime/syntax/network request exceptions /PromiseRejection events, what a call stack is, and how to get the call stack.
Are you looking forward to it even before you start? Ok, everybody hold on to the handrail, the old driver is going to drive
The profile
-
Exception or error? How will it affect our code?
-
What are the built-in exception types?
-
Start writing your own exception types!
-
Try /catch is enough to catch the runtime exception in synchronized code.
-
The “omnipotent” exception catcher window.onerror, really omnipotent?
-
Promise. Reject Also throws exceptions, what to do?
-
404, such as network request abnormality really after sleep?
One. Exception or error? How will it affect our code?
What do you mean by saying that an exception can only cause the current task to terminate? This involves the principle of Event Loop, and I will try to explain it in code.
// 1. The current code block will be pushed into the task queue as a task, and the JavaScript thread will continuously extract the task from the task queue for execution; // 2. If an exception is reported during the execution of the task, and the exception is not captured and processed, the call stack will be thrown from the top to the bottom, and the execution of the current task will be terminated. // 3. The JavaScript thread will continue to extract the next task from the task queue and continue to execute. Function a(){throw Error(“test”)} function b(){a()} b() console.log(” never executed! “) )
// Next task console.log(” You have your exception, I still execute!” )
What are the built-in exception types?
@prop
– Exception name @prop
– Unusual messages for human reading @prop
Constructor – constructor @method toString():
– Displays abnormal information
@
{
}
The script in which the exception occurs
@
{
}
The line number on which the exception occurred @
{
}
The column number where the exception occurred @
{
}
Call stack information when an exception occurs,
@ is supported only when above
ToSource ():String – The content of the script where the exception occurred
@
{
}
和
Almost @
{
}
The number of the exception type. Jumbo sets a unique number for each exception
@constructor@param {String=} message – Set the message attribute @param {String=} fileName – Set the fileName attribute @param {number=} lineNumber – Set the lineNUmber property
- EvalError, an exception that occurs when eval() is called, has been deprecated for backward compatibility only
- InternalError, JavaScript engine internal exception, FireFox only provided!
- RangeError, when crossing the line function arguments to occur, such as Array Number. ToExponential, Number. ToFixed and Number toPrecision into illegal refs.
- ReferenceError occurs when an undeclared variable is referenced
- SyntaxError, a SyntaxError occurred during parsing
- TypeError, null.f() also reports this error when the value is not of the expected type
- URIError, when passing a illegal URI to global URI processing function, such as decodeURIComponent (‘ % ‘), namely decodeURIComponent, decodeURI, encodeURIComponent, encodeURI
Write your own exception types!
So we can just pick it up
function MyError(message, fileName, lineNumber){
(
MyError);
MyError(message, fileName, lineNumber)
.message = message ||
(fileName){
.fileName = fileName }
(lineNumber){
.lineNumber = lineNumber }}
proto = MyError.prototype =
.create(
.prototype)proto.name =
proto.constructor = MyError
(
MyError [& args] (
this (
(
MyError this) (
[ps [
] idxs (
(
(
args) (
ps)) range)] (
(
[accu i] (
accu (
ps i) (
args i)) accu) this idxs)) (
new MyError args))))(
proto (
MyError
(
js/Object (
Error))))(
proto
) (
proto
MyError) four. Try /catch is enough to catch the runtime exception in synchronized code
{
(
)}
(e){
.log(e.message)}
(
(
(
) (
e (
(
e)))))
1.” Synchronous code “means that it cannot get exceptions from asynchronous code such as setTimeout and Promise, that is, try/catch can only catch exceptions from the current task, and asynchronous code such as setTimeout is executed in the next EventLoop.
{ setTimeout(function(){
(
)}, 0)}
(e){
.log(e)}
{ a->b = 1}
(e){
.log(e)}
What can be followed by a throw?
{
}
(e){
.log(e)}
{
(
)}
(e){
(
== e.name){
}
(
== e.name){
}}
Window. onerror is a universal exception catcher.
@
Window. onerror @param {
}
Exception message “@
{
}
Of the script in which the exception occurred
@
{
}
The script number on which the exception occurred @
{
}
The script column where the exception occurred @
{?
}
Instance,
and
Does not have this argument in
.onerror = function(message, source, lineno, colno, error){
}setTimeout(function(){
(
) }, 0)a->b = 1
If onError returns true, the exception will not continue to be thrown up (otherwise it will become an Uncaught Error).
.onerror = function(){
}
- For exceptions reported by cross-domain scripts in Chrome, although onError can catch them, Script Error is reported uniformly. To get the correct error information, cross-domain resource sharing CORS must be configured.
- Window.onerror actually uses the event bubbling mechanism to catch exceptions and is only raised during the bubble phase, so non-bubbling exceptions such as network request exceptions cannot be caught.
- Window.onerror cannot handle exceptions that Promise. Reject generates that are not caught.
Reject also throw exception, how to do?
How does a Promise signal that an exception has occurred?
- Call the promise.reject class method
- Call the Reject method in the factory method
- Throw an exception in a factory method or then callback function
.reject(
)
(function(resolve, reject) { reject(
)})
(function{
})
(function(r) { r(
(
) }).then(function(e) {
e })
Catch exceptions before they happen
(function(resolve, reject){ setTimeout(reject, 0)}).catch(function(e){
.log(
)
}).then(function(x){
.log(x)})
Top-level exception handling that is Promise specific
.addEventListener(
, function(e){
E.preventdefault ()}) a late catch
p =
(function(resolve, reject){ setTimeout(reject, 0)})setTimeout(function(){ p.catch(function(e){
.log(
)
})}, 1000).
.addEventListener(
, function(e){
e.preventDefault()})
Seven.404, such as abnormal network request really after sleep?
.addEventListener(
, function(e){
.log(e.bubbles)
},
)
conclusion
Please note:
www.cnblogs.com/fsjohnhuang…^_^ fat John
reference
stackoverflow.com/questi … …