The examples in this article describe the use of Try catch exception capture mechanisms in JavaScript. Share to everyone for your reference, specific as follows:
1. Like Java, JavaScript also has a try catch block, which is the mechanism for exception capture.
(1) A typical try Catch statement
try{
}
catch{
}
finally{
}
As in Java, the most typical try catch statement in JS is also divided into three parts, and try is used to catch exceptions, catch to handle exceptions, and finally to close resources for subsequent operations.
Example:
try{
Throw "error"
}
catch (ex)
{
console.log (ex);
}
finally{
Console.log ("finally")
}
Console output in turn: error,finally
(2) in try,catch blocks, catch blocks and finally blocks only need one, so the following try catch blocks are also possible
Example:
try {
throw "error"
}
finally{
}
Throw exception only, other case is not an example
(3) A try catch block is included in a try Catch block
try{
try{
Throw "error"
}
finally{
console.log ("Finally1")
}
\
catch (ex)
{
Console.log (ex)
}
finally{
console.log ("Finally2")
}
In the case of nested loops like this, the order of output is: finally1,error,finally2
(4) Nested try catch blocks, throwing exceptions
try{
try{
throw "Error1"
}
catch (ex)
{
console.log (ex);
Throw "Error2"
}
finally{
console.log ("Finally1")
}
catch (ex)
{
Console.log (ex);
}
finally{
console.log ("Finally2")
}
The final output is: error1,finally1,error2,finally2
More readers interested in JavaScript-related content can view the site topics: "JavaScript error and debugging skills Summary", "JavaScript value-handling Skills Summary", "JavaScript Coding Operation Skills Summary", " JavaScript in the JSON Operation tips Summary, "JavaScript switching effects and techniques summary", "JavaScript Search Algorithm Skills Summary", "JavaScript animation effects and Skills summary", "JavaScript data structure and algorithm skills summary , JavaScript traversal algorithms and techniques summary and JavaScript mathematical Operational Usage Summary
I hope this article will help you with JavaScript programming.