Try..catch

Try {
  alert('Start of try runs');  // *!*(1) <--*/!*
  // ...no errors here
  alert('End of try runs');   // *!*(2) <--*/!*
} catch(err) {
  alert('Catch is ignored, because there are no errors'); // (3)
}

alert("...Then the execution continues");
  • First, the code in try {...} is executed.
  • If there were no errors, then catch(err) is ignored: the execution reaches the end of try and then jumps over catch.
  • F an error occurs, then try execution is stopped, and the control flows to the beginning of catch(err). The err variable (can use any name for it) contains an error object with details about what's happened.

Syntax

Try {
  // code...
} catch (err) {
  // error handling
}

Try..catch — Structure map

Clickable & Draggable!

Try..catch — Related pages: