Smal SEO Tool

JavaScript Errors

The try...catch statement allows you to handle errors gracefully without stopping the script.

The try...catch Statement

The try statement allows you to define a block of code to be tested for errors while it is being executed. The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.

javascript
try {
  // Block of code to try
  adddlert("Welcome guest!");
}
catch(err) {
  // Block of code to handle errors
  console.error(err.message);
}

The throw Statement

The throw statement allows you to create a custom error.

javascript
let x = 5;
if(x < 10) throw "Too low";

Test Yourself with an Exercise

Which statement is used to define a block of code to be tested for errors?