Next Topic ⮕
try, catch, finally in JavaScriptYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.
Next Topic ⮕
try, catch, finally in JavaScriptErrors are inevitable in programming. In JavaScript, errors can occur due to typos, invalid operations, missing variables, or unexpected behavior at runtime.
Understanding error types and how to catch them is crucial to writing reliable code. JavaScript offers built-in mechanisms like try...catch
to help developers gracefully handle unexpected issues.
eval()
function (rarely used today).decodeURI()
or encodeURI()
are misused.This error is thrown when you try to access a variable that hasn’t been declared.
// ReferenceError Example
try {
console.log(userName); // userName is not defined
} catch (error) {
console.error("Caught a ReferenceError:", error.message);
}
Output:
Caught a ReferenceError: userName is not defined
Thrown when a value is not of the expected type. For instance, calling a method on undefined
.
// TypeError Example
try {
let num = 42;
num.toUpperCase(); // toUpperCase() is a string method
} catch (error) {
console.error("Caught a TypeError:", error.message);
}
Output:
Caught a TypeError: num.toUpperCase is not a function
Occurs when the code structure is not valid JavaScript. These errors must be caught at eval-time.
// SyntaxError Example using eval
try {
eval('function () {'); // invalid syntax
} catch (error) {
console.error("Caught a SyntaxError:", error.message);
}
Output:
Caught a SyntaxError: Unexpected token ')'
Thrown when a number is outside of its allowable range, e.g., when creating an array with invalid length.
// RangeError Example
try {
let items = new Array(-5);
} catch (error) {
console.error("Caught a RangeError:", error.message);
}
Output:
Caught a RangeError: Invalid array length
Happens when decodeURI
or encodeURI
is used incorrectly.
// URIError Example
try {
decodeURI('%'); // invalid URI component
} catch (error) {
console.error("Caught a URIError:", error.message);
}
Output:
Caught a URIError: URI malformed
JavaScript provides try...catch
to handle errors:
try {
// risky code here
} catch (error) {
// handle error here
console.log("Error caught:", error.message);
} finally {
console.log("This block always runs");
}
If errors are not caught using try...catch
, JavaScript will stop execution and show the error in the console. In production, this could mean breaking your entire web page or script.
Q: Can all errors be caught with try...catch?
A: No, SyntaxError
that occurs outside eval
cannot be caught using try...catch
. Also, asynchronous errors need special handling (like .catch()
for Promises).
Q: Can you throw custom errors?
A: Yes, using throw
. You can throw a string, number, object, or an instance of Error
.
try {
throw new Error("This is a custom error");
} catch (e) {
console.log("Custom Error:", e.message);
}
Output:
Custom Error: This is a custom error
By understanding error types and handling them effectively, you can make your JavaScript programs more robust and user-friendly. Mastering try...catch
and identifying error patterns is an essential skill for every developer.
Next Topic ⮕
try, catch, finally in JavaScriptYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.