This paper analyzes the use of try...catch...finally in JavaScript, and share it for everyone's reference, the specific contents are as follows
A little more complex a little bit, it is necessary to use the judgment statement, if else to make conditional judgments, say if conditions else otherwise, such a judgment for the code to write code Lennon is very familiar with.
If you think this is also very simple, you might use a mixed if else conditional judgment statement plus a try catch to handle the statement, although you can handle any object with a try catch, throw a statement with an error by throw, and catch the throw of the object or the object's error. Today we only say Try...catch, the following example throws an array, a time, a prototype function, a numeric type, and so on.
function Trycatch () {
var array = [234],
newdate = new Date (),
fun = function () {}, is
= 12.22,
call;< c6/>try{
throw array + ' \ n ' + newdate.tolocalestring () + ' \ n ' + fun.prototype.constructor +
' \ n ' + (typeof is = = ' number ') + ' \ n ' + call; Be careful. There is also an ' e
'
catch (e) {
console.log (e) on the back of the local
finally{
console.log (' err finally ');
}
Trycatch ()//
output:
//234
//2015/10/12 PM 10:07:03
//function () {}
//TRUE//
undefined
To be more precise, put a statement that might produce an error inside the try. When the try statement starts executing and throws an error, the catch executes the internal statement and the error message in the corresponding try. When a finally statement is executed, the finally statement is executed only after the try and catch statements have been executed, regardless of whether a try throws an exception or catch capture executes the finally statement.
function Trycatch () {
try{
throw new Error (' Koringz ');
}
catch (e) {
console.log (e.message);
}
finally{
console.log (' err finally ');
}
Trycatch ()//
output:
//Koringz
//Err finally
With a try throw out a wrong statement, we see the catch caught in a wrong message//Koringz, but the same finally also output/err finally. Although we know how to handle a try catch workflow, but do not understand the code handlers of finally blocks, as we have always thought about finally statements, the finally output is not constrained and constrained by try and catch. Here are some of the finally output demo codes:
function Trycatch () {
try{
throw new Error (' Koringz ');
}
finally{
console.log (' err finally ');
Return Console.log (' new finally ')
}
Trycatch ()
//Err finally
/new finally
As shown above, the try throws a wrong statement, finally the result of the output is://Err finally//new finally.
function Trycatch () {
try{
throw new Error (' Koringz ');
}
catch (e) {
console.log (' err finally ');
Return Console.log (' new finally ')
}
Trycatch ()
//Err finally
/new finally
As shown above, the try throws an error statement, catch catches the error output, and finally. Err finally//new finally.
When I modify a try statement:
function Trycatch () {
try{
//
}
catch (e) {
console.log (' err finally ');
Return Console.log (' new Finally '
}}
Trycatch ()
//Empty (Viod)
//Empty (Viod)
As a result, the output is empty. Empty (Viod). Because the try did not throw the error, the catch did not catch the exception, so the output is empty.
So let's take a look at the following case, which may give you a better idea of the exception handling of the try Catch statement, as shown in the following example.
try{
try{
throw new Error (' Open ');
}
catch (e) {
console.info (e.message);
Throw e
}
finally{
console.log (' finally ');
}
catch (e) {
console.log (' op ', e.message);
}
Open
//finally
//OP Open
When we nest a try catch in a block of code blocks that might throw an error, throw a statement throw new error (' Open ') through a nested code block try, and immediately after the nested try passes the error to the nested catch handle. After finally running through the nested finally run, we see the last Result//Op open, in fact, nested catch-caught error messages are thrown at the outermost catch capture. OP Open
In other words: Any given exception is only caught once by the enclosing catch block closest to it.
Of course, any new exception thrown in the "inside" block (because the code in the catch block can also throw an exception) will be captured by the "outside" block.
The above is the entire content of this article, I hope to learn JavaScript program to help you.