Exceptions

try{
   throw "Help, application is crashed!!";
}catch(err){
   alert("Dear user, error occur: "+err);
}finally{
   console.log("finally block executed");
}
  • try - defines a test block
  • catch - defines a block of error handling
  • finally - defines a block that will be executed regardless of the test result
  • throw - throws an exception with given parameter, parameter can be any value (usually integer as error code, string as message, or a Error object)

Error

The Error object provides information about error. It has two properties:

  • name - name of error, there are several predefined names:
    • EvalError - a error has occurred in the eval() function
    • RangeError - a number "out of range" has occurred
    • ReferenceError - an illegal reference has occurred
    • SyntaxError - a syntax error has occurred
    • TypeError - a type error has occurred
    • URIError - an error in encodeURI() has occurred
  • message - message of error
try{
   throw  new Error("Help, application is crashed!!");
}catch(err){
   //result: name=Error msg=Help, application is crashed!!
   console.log(`name=${err.name} msg=${err.message}`);
}