“This is the 18th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”
No matter how proficient you are in programming, sometimes our scripts will go wrong. This could be because of coding errors, different user input than expected, incorrect server responses, and thousands of other reasons.
Usually, if an error occurs, the script will “die” (stop immediately), and if you don’t want the script to stop directly, you can use try.. Catch, to “catch” an error.
try.. Catch the grammar
try.. The catch structure consists of two parts: try and catch:
Try {// code... } catch (err) {// Error catch}Copy the code
💡 Procedure
- To perform first
try {... }
The code in - No errors, ignore
catch(err)
: perform totry
“And skip the endcatch
Continue execution. - An error occurred and execution stopped
try
And control the flow directioncatch(err)
In the beginning. variableerr
(we can use any name) will contain an Error object that contains details of the event that occurred.
So try {… An error in the} block will not stop the script, and you can handle it in a catch.
try.. The failure of a catch
The code itself is not executable
try.. Catch is only useful for valid JavaScript code, so what does that mean?
🎨 The following is an example:
- If the error is undefined, it can be caught;
- But syntax errors like this one with extra curly braces will not work.
Asynchronous operation
If an exception occurs in “scheduled” code, such as in setTimeout, then try.. Catch does not catch an exception:
🎨 example:
You can see that in the following example run, the undefined variable error is not caught, but is reported as an error. This is because try.. The catch wraps around the function that is scheduled to execute, and the function itself will be executed later, when the engine has left the try.. Structure of the catch.
💡 improvement
If you want to catch exceptions in scheduled functions, try.. The catch must be inside this function
The Error object in catch
When an error occurs, JavaScript generates an object that contains detailed information about it. The object is then passed as an argument to the catch
An error object in a catch has two main properties:
name
The name of the Error. For example, for an undefined variable, the name is"ReferenceError"
.message
A detailed textual description of error.
There are other non-standard attributes available in most environments. Among the most widely used and supported are:
stack
Current call stack: A string for debugging purposes that contains information about the nested sequence of calls that caused the error.
Throw statement
The throw statement is used to throw a user-defined exception. Execution of the current function is stopped (statements after the throw will not execute), and control is passed to the first catch block in the call stack. If there is no catch block in the caller function, the program terminates.
🎨 example:
Technically, you can use anything as an error object.
throw "Error2"; // throw an exception with a string value: throw 42; // throw true with an integer value of 42; // An exception is thrown with a value of trueCopy the code
But it’s better to use objects, preferably objects with name and message attributes (somewhat compatible with built-in Errors).
try… catch… finally
try.. Catch structures may also have a code clause: finally.
try { ... The code trying to execute... } catch(e) { ... To deal with the error... } finally { ... Code that always executes... }Copy the code
The finally clause is usually used for: When we start something, we want to finish something no matter what happens.
try.. finally
try.. The finally structure can also be executed normally, and can be used when you do not want to handle error here, but need to ensure that the initiated processing is completed.
Function func() {// start the operation that needs to be done (such as measuring) try {//... } finally {// Do what we need to do before, even if the execution in the try fails}}Copy the code
This code has no catch, so an error in a try always jumps code execution out of func(). However, you need to execute the code in Finally before jumping out.
References:
MDN try… catch
Error handling
🎨 [thumbs up] [concerns] don’t get lost, more front-end dry goods waiting for you to unlock
Phase to recommend
👉 Product, technology, design and other areas of the Internet “basic terms” literacy
👉 Web security defense means are here!
👉 7 types of JavaScript to fill in the gaps!
👉 JavaScript deep copy and shallow copy read this article is enough!