how to throw an error in a try with a message and catch it using js code example
Example 1: catch error message js
try {
// test code
} catch (error) { // if error
console.error(error); // return error
}
Example 2: try catch throwing error in javascript
let json = '{ "age": 30 }'; // incomplete data
try {
let user = JSON.parse(json); // <-- no errors
alert( user.name ); // no name!
} catch (e) {
alert( "doesn't execute" );
}