Definition and Usage
try / catch / finally statements to handle error codes that may occur.
Error may be a syntax error, the programmer usually caused by coding errors or typos. They may be misspelled or missing language features (possibly due to browser differences).
try statement allows us to define the code block error test at the time of execution.
catch statement allows us to define when a try code block when code block error occurs performed.
finally statement will be executed regardless of whether the abnormal after the try and catch.
Note: You must use at least one time catch and finally statements are optional, but you try to use the statement.
Tip: When an error occurs, JavaScript execution stops, and generates an error message. Use the throw statement to create a custom message (throw an exception). If you throw and the try , the catch used together, you can control the output of the error message program.
let x = [1,12,6,'']; x.map(v => { try { if(v < 5) throw "太小"; if(v > 10) throw "太大"; if (isNaN (v)) throw "not a number"; if(v == "") throw "为空" } catch(err) { console.log('catch '+v) } finally { console.log ( 'the statement will be executed regardless of whether the abnormal after the try and catch') } })