try / catch / finally / throw
A syntax for catching errors that occur during program execution and handling them appropriately. It lets you prevent the program from stopping when an error occurs.
Syntax
try {
// Code that might throw an error
} catch (error) {
// Code to handle the error
} finally {
// Code that always runs, regardless of whether an error occurred
}
// Intentionally throw an error.
throw new Error("error message");
Parts of the syntax
| Syntax | Description |
|---|---|
| try | A block containing code that might throw an error. If an error occurs inside it, control immediately transfers to the following catch block. |
| catch (error) | A block that runs when an error occurs in the try block. The error argument receives the error object, and you can retrieve the error message with error.message. |
| finally | Optional. A block that always runs regardless of whether an error occurred. Use it for cleanup tasks such as releasing resources. |
| throw | Intentionally throws an error. Use it when you want to raise an error explicitly, such as when validating input values or detecting an invalid value. |
Common error types
| Error | Description |
|---|---|
| Error | The base object for all errors. Used as throw new Error("message"). |
| TypeError | Thrown when a value is not of the expected type. For example, accessing a property on null or undefined, such as null.length. |
| ReferenceError | Thrown when referencing a variable that does not exist. |
| SyntaxError | Thrown when there is a syntax error. Also thrown when an invalid JSON string is passed to JSON.parse(). |
| RangeError | Thrown when a value is outside the allowed range. For example, specifying a negative value for the size of an array. |
Sample code
// Basic try...catch usage
try {
var data = JSON.parse("invalid JSON string");
} catch (error) {
console.log("Error type: " + error.name); // Outputs "Error type: SyntaxError".
console.log("Message: " + error.message);
}
// Using the finally block
console.log("Starting process");
try {
var result = 10 / 0;
console.log("Result: " + result); // Outputs "Result: Infinity". Division by zero does not throw an error in JavaScript.
} catch (error) {
console.log("Error: " + error.message);
} finally {
console.log("Process complete"); // Always runs regardless of whether an error occurred.
}
// Throwing an error intentionally with throw
function divide(a, b) {
if (b === 0) {
throw new Error("Cannot divide by zero"); // Intentionally throws an error.
}
return a / b;
}
try {
var answer = divide(10, 0);
} catch (error) {
console.log(error.message); // Outputs "Cannot divide by zero".
}
// TypeError example
try {
var obj = null;
console.log(obj.name); // Cannot access a property of null.
} catch (error) {
console.log(error.name); // Outputs "TypeError".
console.log(error.message);
}
Overview
try...catch is a syntax for catching errors that occur during program execution and handling them appropriately. When an error occurs inside the try block, the program would normally stop, but by catching the error in the catch block, you can allow execution to continue.
The finally block always runs regardless of whether an error occurred, making it suitable for cleanup tasks such as closing files or disconnecting network connections. You can also use try...finally without a catch block, but it is common practice to include both catch and finally.
throw is a statement for intentionally throwing an error, and is used for argument validation in functions or input value checking. try...catch can only catch runtime errors. Syntax errors are detected before the code runs and cannot be caught by try...catch. However, when code parses syntax at runtime — such as JSON.parse() — a SyntaxError can be caught.
Browser Compatibility
4 or earlier ×
3 or earlier ×
Android Browser
37+ ○
4 or earlier ×
Chrome Android
36+ ○
17 or earlier ×
Firefox Android
79+ ○
3 or earlier ×If you find any errors or copyright issues, please contact us.