Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Try-catch for synchronous errors in Node.js
📖 Scenario: You are building a simple Node.js script that processes user input. Sometimes, the input might cause errors. You want to safely handle these errors so your program does not crash.
🎯 Goal: Build a Node.js script that uses try-catch to handle synchronous errors when parsing JSON input.
📋 What You'll Learn
Create a variable with a JSON string
Create a variable to hold the parsed object
Use try-catch to parse the JSON string safely
In the catch block, assign an error message to the parsed object variable
💡 Why This Matters
🌍 Real World
Handling user input or external data safely is important to avoid crashes in Node.js applications.
💼 Career
Error handling with try-catch is a fundamental skill for backend developers working with Node.js to build robust applications.
Progress0 / 4 steps
1
Create a JSON string variable
Create a variable called jsonString and set it to the string '{"name": "Alice", "age": 30}'.
Node.js
Hint
Remember to use single quotes around the whole string and double quotes inside for JSON keys and values.
2
Create a variable to hold parsed data
Create a variable called parsedData and set it to null.
Node.js
Hint
Use let because we will change this variable later.
3
Use try-catch to parse JSON safely
Write a try-catch block. Inside try, parse jsonString using JSON.parse and assign it to parsedData. In catch, assign the string 'Error parsing JSON' to parsedData.
Node.js
Hint
Use try { ... } catch (error) { ... } syntax and assign inside each block as instructed.
4
Complete the script with a safe JSON parse
Ensure the full script includes the jsonString variable, the parsedData variable initialized to null, and the try-catch block that parses jsonString safely and assigns the result or error message to parsedData.
Node.js
Hint
Make sure all parts are included exactly as before to complete the script.
Practice
(1/5)
1. What is the main purpose of using try-catch in Node.js?
easy
A. To handle errors that happen during code execution without stopping the program
B. To speed up the execution of asynchronous code
C. To declare variables that are only used inside a block
D. To format output text in the console
Solution
Step 1: Understand the role of try-catch
Try-catch is used to catch errors that happen during the running of code so the program can continue safely.
Step 2: Eliminate unrelated options
Options about speeding asynchronous code, declaring variables, or formatting output do not relate to error handling.
Final Answer:
To handle errors that happen during code execution without stopping the program -> Option A
Quick Check:
Error handling = D [OK]
Hint: Try-catch is for catching errors, not for speed or formatting [OK]
Common Mistakes:
Thinking try-catch speeds up code
Confusing try-catch with variable declaration
Using try-catch to format output
2. Which of the following is the correct syntax to catch synchronous errors in Node.js?
JSON.parse throws an error if input is invalid, so we must catch it to avoid crashing.
Step 2: Check each option's error handling
try {
return JSON.parse(input);
} catch (e) {
return null;
} uses try-catch correctly and returns null on error. if (JSON.parse(input)) {
return JSON.parse(input);
} else {
return null;
} does not catch errors, causing crash. try {
JSON.parse(input);
} catch {
return null;
} misses error parameter in catch and does not return parsed value. return JSON.parse(input) || null; does not catch errors and will crash on invalid input.