1try {
2 // Try to run this code
3}
4catch(err) {
5 // if any error, Code throws the error
6}
7finally {
8 // Always run this code regardless of error or not
9 //this block is optional
10}
1var someNumber = 1;
2try {
3 someNumber.replace("-",""); //You can't replace a int
4} catch(err) {
5 console.log(err);
6}
1//Catch is the method used when your promise has been rejected.
2//It is executed immediately after a promise's reject method is called.
3//Here’s the syntax:
4
5
6myPromise.catch(error => {
7 // do something with the error.
8});
9
10//error is the argument passed in to the reject method.
11
1One should avoid throw errors as the way to pass error conditions
2around in applications.
3
4The throw statement should only be used
5"For this should never happen, crash and burn. Do not
6recover elegantly in any way"
7
8try catch however is used in situation where host objects
9or ECMAScript may throw errors.
10
11Example:
12-------------------------------------
13var json
14try {
15 json = JSON.parse(input)
16} catch (e) {
17 // invalid json input, set to null
18 json = null
19}
20-------------------------------------