Get a Quote Right Now

Edit Template

Exception Handling

You can throw exceptions using the throw statement and handle them using the try...catch statements.

throw statement:-

throw "Error2"; // String type
throw 42; // Number type
throw true; // Boolean type
throw {
toString() {
return "I'm an object!";
},
};

try...catch statement:-

openMyFile();
try {
  writeMyFile(theData); // This may throw an error
} catch (e) {
  handleError(e); // If an error occurred, handle it
} finally {
  closeMyFile(); // Always close the resource
}

Share

Leave a Reply

Your email address will not be published. Required fields are marked *