Robust error handling helps your program stay strong and keep working even when things go wrong. It stops crashes and shows clear messages to fix problems quickly.
When reading files that might not exist or be accessible
When making network requests that could fail or timeout
When working with user input that might be incorrect or missing
When calling external services that might return errors
When running code that depends on resources that may not be ready
Syntax
Node.js
try {
// code that might cause an error
} catch (error) {
// code to handle the error
} finally {
// code that runs no matter what
}
Use try to wrap code that might fail.
Use catch to handle errors gracefully.
Examples
This example catches a JSON parsing error and logs a friendly message.
Node.js
try {
const data = JSON.parse('invalid json');
} catch (error) {
console.error('Failed to parse JSON:', error.message);
}
Here, any error from riskyFunction is caught and a simple message is shown.
Node.js
try {
const result = riskyFunction();
} catch (error) {
console.log('Oops! Something went wrong.');
}
You can also throw a new error with a custom message inside catch.
This Node.js program tries to read a file asynchronously. If the file is missing or unreadable, it catches the error and logs a clear message. The finally block runs regardless, showing a completion message.
A. Throwing a string instead of an Error object is invalid.
B. Missing error parameter in catch block parentheses.
C. The try block must not contain console.log statements.
D. Catch block must be followed by finally block.
Solution
Step 1: Check catch block syntax
In Node.js, catch must have parentheses with an error parameter, e.g., catch (error).
Step 2: Validate other parts
Throwing a string is allowed, console.log is fine, and finally is optional.
Final Answer:
Missing error parameter in catch block parentheses. -> Option B
Quick Check:
Catch needs error param = B [OK]
Hint: Catch always needs (error) parameter in Node.js [OK]
Common Mistakes:
Thinking catch can omit error parameter
Believing throw must use Error object only
Assuming finally block is mandatory
5. You have a Node.js function that reads a file and processes its content. Which approach best ensures robust error handling to keep the app stable and inform users properly?
hard
A. Use multiple nested try blocks without catch to isolate errors.
B. Ignore errors during file reading to avoid interrupting the process.
C. Only catch errors during processing, not during file reading.
D. Use try-catch around file reading and processing, log errors clearly, and send user-friendly messages.
Solution
Step 1: Identify where errors can occur
Errors may happen during file reading or processing, so both need handling.
Step 2: Choose error handling strategy
Using try-catch around both ensures catching all errors, logging them, and informing users clearly.
Final Answer:
Use try-catch around file reading and processing, log errors clearly, and send user-friendly messages. -> Option D
Quick Check:
Catch all errors + clear logs + user messages = D [OK]
Hint: Catch all errors and inform users clearly [OK]
Common Mistakes:
Ignoring errors thinking they are rare
Catching only some errors, missing others
Using try without catch blocks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.