Before, I always like to use Console.log (do some thing) to perform the output type and value, want to see the popup immediately, it will be directly in the browser alert (), these are the basics.
A little more complex, it is necessary to use the judgment statement, if else to determine the condition, say if condition else otherwise, such a judgment for the code Lennon is already very familiar with writing the program.
If you think this is also very simple, you may use the mixed if else conditional judgment statement plus a try catch to process the statement, although with a try catch can handle any object, throw a wrong statement by throw, and then catch throws the object or the object error, Today we only say Try...catch, the following examples throw arrays, time, prototype functions, numeric types, and so on.
functionTrycatch () {varArray = [234], Newdate=NewDate (), fun=function() {}, is= 12.22, call; Try{ ThrowArray + ' \ n ' + newdate.tolocalestring () + ' \ n ' + fun.prototype.constructor + ' \ n ' + (typeofis = = = ' number ') + ' \ n ' + call;//careful, there's an ' e ' behind the local . } Catch(e) {console.log (e); } finally{Console.log (' Err finally '); }}trycatch ()
// Output: //234//2015/10/12 pm 10:07:03//function () {}//true//undefined
more accurately , try to put a statement that could produce an error. When a try statement starts executing and throws an error, the catch executes the inner statement and the corresponding try error message in the message. When the finally statement is executed, the finally statement is executed only after the try statement and the catch statement are executed, regardless of whether the try throws an exception or the catch capture executes the finally statement.
function Trycatch () { try throw new Error (' Koringz ' ); catch (E) {console.log (e.message); finally {console.log ( ' err F Inally ' ); }}trycatch () // output: // Koringz // err finally
The
throws an incorrect statement through a try, and we see that the catch captures an incorrect message//Koringz, but the same finally also outputs the/ERR finally. While we understand how the try catch workflow is handled, we do not understand the code handlers of the finally block, as we have always thought of the finally statement, which is that the finally output is not constrained by try and catch. Here are a few output demo codes for finally:
function Trycatch () { try{ thrownew 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, and the result of the finally output is://Err finally//new finally.
function Trycatch () { try{ thrownew Error (' Koringz '); } Catch (e) { console.log (' err finally '); return console.log (' new finally ') }}trycatch ()// err finally New finally
As shown above, the try throws a wrong statement, and the catch catches the error output to the end. 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 ()// null (viod)// Empty (viod)
As a result, the output is empty. Empty (Viod). Because the try does not throw an error, the catch does not catch an exception, so the output is empty.
So let's take a look at the following example, which might give you a better understanding of the exception handling of a try catch statement.
Try{ Try{ Throw NewError (' Open '); } Catch(e) {console.info (e.message); Throwe}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 that may throw an error in a try, throw a statement that may be wrong in a nested code block try to throw a new error (' Open '), and immediately after the nested try passes the error to the nested catch processing, Finally, after the nested finally run, we see the last Result//Op open, in fact the error message of the nested catch catch is thrown to the outermost catch capture. OP Open
That is, any given exception will only be captured once by the enclosing catch block closest to it.
Of course, any new exceptions thrown in the "inner" block (because the code in the catch block can also throw exceptions) will be captured by an "external" block.
Reference: Https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/try...catch
Reference: Https://msdn.microsoft.com/library/4yahc5d8 (v=vs.94). aspx
JavaScript: Understanding Try...catch...finally