Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

JavaScript try and catch

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.

The JavaScript statements try and catch come in pairs:

try {
  Block of code to try
}
catch(err) {
  Block of code to handle errors
}

JavaScript Throws Errors

When an error occurs, JavaScript typically stops execution and generates an error message.

The technical term for this is that JavaScript will “throw an exception” (or “throw an error”).

JavaScript creates an Error object that contains two properties: name and message.