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}
1try {
2 nonExistentFunction();
3} catch (error) {
4 console.error(error);
5 // expected output: ReferenceError: nonExistentFunction is not defined
6 // Note - error messages will vary depending on browser
7}
1try {
2 // test code
3} catch (error) { // if error
4 console.error(error); // return error
5}
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