This article mainly helps you understand try in javascript... catch... finally, Step by Step Master javascript try... catch... finally's usage, interested friends can refer to this article for your analysis of javascript try... catch... the usage of finally is for your reference. The specific content is as follows:
If else is a little more complex, it is necessary to use a judgment statement. if else is used to determine the condition, then if condition else is not used. Such a judgment is very familiar with coding.
If you think this is also very simple, you may use the mixed if else condition judgment statement and try catch to process the statement. Although try catch can be used to process any object, throw a wrong statement and catch the object to throw the error. Today we only talk about try... the following example throws an array, time, prototype function, and number type.
Function trycatch () {var array = [234], newdate = new Date (), fun = function () {}, is = 12.22, call; try {throw array + '\ n' + newdate. toLocaleString () + '\ n' + fun. prototype. constructor + '\ n' + (typeof is = 'number') +' \ n' + call; // be careful that there is an 'e'} catch (e) after the local) {console. log (e);} finally {console. log ('err finally ');} trycatch () // output: // 234 // 10:07:03 // function () {}// true // undefined
More accurately, put a statement in a try that may generate an error. When a try statement is executed and an error is thrown, the catch statement executes the internal statement and the error message in the corresponding try statement. When to execute the finally statement, the finally statement is executed only after the try statement and catch statement are executed. The finally statement is executed no matter whether the try throws an exception or catch.
Function trycatch () {try {throw new Error ('koringz');} catch (e) {console. log (e. message);} finally {console. log ('err finally ');} trycatch () // output: // koringz // err finally
If you try to throw an incorrect statement, we can see that the catch caught an error message // koringz, but the same finally output // err finally. Although we understand how to process a try catch workflow, we do not know the finally block code processing program. In the past, we consistently thought about finally statements, the finally output is not restricted by try and catch. Below are several 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, try to throw an incorrect statement. The finally output result 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, try to throw an incorrect statement and catch the error output result as same as finally. // Err finally // new finally.
When I modify the try statement:
Function trycatch () {try {//} catch (e) {console. log ('err finally '); return console. log ('new finally ')} trycatch () // null (viod)
The output is empty. // Null (viod ). Because try does not throw an error, catch does not catch the exception, so the output result is null.
Let's take a look at the following case. The following example may help you better understand the Exception Handling of try catch statements.
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 try catch is nested in a code block that may cause an Error in try, throw new Error ('open') is thrown through the nested code block try ');, next, the nested try will pass the error to the nested catch process. After the nested finally run, we will see the last result // op open, in fact, the nested catch error information is thrown to the outermost catch. // Op open
That is to say, any given exception is captured only once by the closed catch Block closest to it.
Of course, any new exceptions thrown in the "internal" block (because the code in the catch block can also throw exceptions) will be captured by the "external" block.
The above is all the content of this article, and I hope it will help you learn javascript programming.