Error Handling in JavaScript
Error handling in JavaScript allows developers to manage unexpected conditions
in a program gracefully, ensuring that the application continues running smoothly
and providing useful feedback to the user.
1. Types of Errors
JavaScript has several types of errors, which can be broadly categorized as:
Syntax Errors: Occur when the code violates JavaScript's grammar rules.
const name = "John' // SyntaxError: Unexpected token
Reference Errors: Occur when trying to reference a variable that is not
defined.
console.log(nonExistentVar); // ReferenceError: nonExistentVar is not
defined
Type Errors: Occur when a value is not of the expected type.
null.foo(); // TypeError: Cannot read property 'foo' of null
Range Errors: Occur when a value is out of range.
let numbers = new Array(-1); // RangeError: Invalid array length
Eval Errors: Occur when using the eval() function inappropriately.
eval('alert("test")'); // EvalError: Invalid or illegal use of eval()
2. Try...Catch
The try...catch statement allows you to test a block of code for errors and handle
those errors gracefully.
, Syntax:
try {
// Code that may throw an error
} catch (error) {
// Code that handles the error
}
Example:
try {
const result = someUndefinedFunction();
} catch (error) {
console.log("An error occurred: " + error.message);
}
try block: Contains the code that may cause an error.
catch block: Contains the code that handles the error, typically logging the
error message or providing a fallback behavior.
The catch block receives an Error object, which has properties like name,
message, and stack.
Handling Specific Errors:
You can catch specific types of errors by checking the name property of the error
object.
try {
const result = someUndefinedFunction();
} catch (error) {
if (error instanceof ReferenceError) {
console.log("Reference Error: " + error.message);
} else {
console.log("An unexpected error occurred: " + error.message);
}
}
Error handling in JavaScript allows developers to manage unexpected conditions
in a program gracefully, ensuring that the application continues running smoothly
and providing useful feedback to the user.
1. Types of Errors
JavaScript has several types of errors, which can be broadly categorized as:
Syntax Errors: Occur when the code violates JavaScript's grammar rules.
const name = "John' // SyntaxError: Unexpected token
Reference Errors: Occur when trying to reference a variable that is not
defined.
console.log(nonExistentVar); // ReferenceError: nonExistentVar is not
defined
Type Errors: Occur when a value is not of the expected type.
null.foo(); // TypeError: Cannot read property 'foo' of null
Range Errors: Occur when a value is out of range.
let numbers = new Array(-1); // RangeError: Invalid array length
Eval Errors: Occur when using the eval() function inappropriately.
eval('alert("test")'); // EvalError: Invalid or illegal use of eval()
2. Try...Catch
The try...catch statement allows you to test a block of code for errors and handle
those errors gracefully.
, Syntax:
try {
// Code that may throw an error
} catch (error) {
// Code that handles the error
}
Example:
try {
const result = someUndefinedFunction();
} catch (error) {
console.log("An error occurred: " + error.message);
}
try block: Contains the code that may cause an error.
catch block: Contains the code that handles the error, typically logging the
error message or providing a fallback behavior.
The catch block receives an Error object, which has properties like name,
message, and stack.
Handling Specific Errors:
You can catch specific types of errors by checking the name property of the error
object.
try {
const result = someUndefinedFunction();
} catch (error) {
if (error instanceof ReferenceError) {
console.log("Reference Error: " + error.message);
} else {
console.log("An unexpected error occurred: " + error.message);
}
}