Exception handling is an essential technique in programming
Exception handling is an essential technique in programming that helps you manage unexpected errors in your code without causing the program to crash. In JavaScript, you can handle exceptions using try
, catch
, and finally
blocks to catch errors, handle them gracefully, and execute any necessary cleanup.
- Exceptions: An exception is an error that occurs during a program's execution. It disrupts the program's normal flow and must be handled to maintain smooth operation.
- Unchecked errors: These are errors that occur when you don’t validate input or data, leading to unexpected behavior.
- Defensive programming: Writing code that anticipates possible errors and exceptions, helping prevent the occurrence of unchecked errors.
- try block: Wraps the code that might throw an exception. If an error occurs in the
try
block, the program jumps to thecatch
block.
try {
// Code that may cause an error
}
- catch block: Handles any error that occurs in the
try
block. Thecatch
block receives the error as an argument, allowing you to analyze or respond to it.
catch (error) {
// Code to handle the error
}
- finally block: Executes regardless of whether an error was caught, often used for cleanup or releasing resources.
finally {
// Code that always runs after try and catch
}
Create a function that makes a simulated API request to fetch user data. Implement a try-catch-finally
block to handle potential errors (like network failures) and provide a defensive check on the API response to prevent further issues. Display an error message if the request fails and a success message if it’s successful.
function fetchUserData() {
try {
let success = Math.random() > 0.5;
setTimeout(() => {
if (!success) {
throw new Error("Failed to fetch user data. Network error.");
} else {
console.log("User data fetched successfully.");
}
}, 1000);
} catch (error) {
console.log("Error:", error.message);
} finally {
console.log("Fetch user data operation complete.");
}
}
fetchUserData();
Create a function that validates a user's name, age, and email. If any validation fails, throw an error and handle it with try-catch-finally
. Use defensive programming techniques to prevent unchecked errors and log a final completion message regardless of validation success or failure.
function validateUserForm(userData) {
try {
if (typeof userData.name !== "string" || userData.name.trim() === "") {
throw new Error("Invalid name. Name cannot be empty.");
}
if (typeof userData.age !== "number" || userData.age <= 0 || !Number.isInteger(userData.age)) {
throw new Error("Invalid age. Age must be a positive integer.");
}
if (!userData.email.includes('@')) {
throw new Error("Invalid email format.");
}
console.log("User form is valid: ", userData);
} catch (error) {
console.log("Validation Error:", error.message);
} finally {
console.log("Validation process completed.");
}
}
// Example test cases
validateUserForm({ name: "Alice", age: 25, email: "[email protected]" });
validateUserForm({ name: "", age: 25, email: "[email protected]" });
validateUserForm({ name: "Alice", age: -1, email: "[email protected]" });
validateUserForm({ name: "Alice", age: 25, email: "alice.com" });
Using try
, catch
, and finally
blocks in JavaScript is essential for gracefully handling unexpected errors, especially when user input or external data might cause problems. By wrapping code in try
blocks, handling potential issues with catch
, and using finally
for cleanup or consistent logging, you can prevent crashes and create a smoother user experience. Additionally, applying defensive programming principles—such as input validation, type checking, and setting default values—further enhances error handling, leading to more robust and resilient applications.