try catch javascript

try {
  // Try to run this code 
catch(err) {
  // if any error, Code throws the error
finally {
  // Always run this code regardless of error or not
  //this block is optional
}//Catch is the method used when your promise has been rejected.
//It is executed immediately after a promise's reject method is called.
//Here’s the syntax:

myPromise.catch(error => {
  // do something with the error.

//error is the argument passed in to the reject method.
One should avoid throw errors as the way to pass error conditions
around in applications.

The throw statement should only be used
"For this should never happen, crash and burn. Do not
recover elegantly in any way"

try catch however is used in situation where host objects
or ECMAScript may throw errors.

var json
try {
    json = JSON.parse(input)
} catch (e) {
    // invalid json input, set to null
    json = null
-------------------------------------var someNumber = 1;
try {
  someNumber.replace("-",""); //You can't replace a int
} catch(err) {
}"The try...catch statement marks a block of statements to try and specifies"
"a response should an exception be thrown."

try {
} catch (error) {
  // expected output: ReferenceError: nonExistentFunction is not defined
  // Note - error messages will vary depending on browser
try {

  alert('Start of try runs');  // (1) <--

  lalala; // error, variable is not defined!

  alert('End of try (never reached)');  // (2)

} catch(err) {

  alert(`Error has occurred!`); // (3) <--


Also in JavaScript: