0
0
Node.jsframework~20 mins

Why robust error handling matters in Node.js - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Error Handling Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use try-catch blocks in Node.js?

What is the main reason to use try-catch blocks in Node.js code?

ATo automatically fix bugs in the code
BTo speed up the execution of asynchronous code
CTo catch runtime errors and prevent the program from crashing unexpectedly
DTo make the code run without any errors even if there are syntax mistakes
Attempts:
2 left
💡 Hint

Think about what happens when an error occurs during program execution.

component_behavior
intermediate
2:00remaining
Output of unhandled promise rejection

What happens when a promise in Node.js is rejected but there is no catch handler?

Node.js
Promise.reject(new Error('Failed')).then(() => console.log('Success'));
AThe program logs 'Success' and continues running
BNode.js emits an unhandledRejection warning and may crash depending on settings
CThe program silently ignores the rejection and continues
DThe program automatically retries the promise
Attempts:
2 left
💡 Hint

Think about what Node.js does when a promise rejection is not handled.

🔧 Debug
advanced
2:00remaining
Identify the error handling mistake

What is wrong with this Node.js code snippet regarding error handling?

Node.js
const fs = require('fs');

fs.readFile('file.txt', (err, data) => {
  console.log(data.toString());
});
AIt does not check for the error <code>err</code> before using <code>data</code>, which can cause a crash
BIt uses a synchronous method instead of asynchronous
CIt should use <code>try-catch</code> instead of a callback
DIt logs the data twice
Attempts:
2 left
💡 Hint

Consider what happens if err is not null.

📝 Syntax
advanced
2:00remaining
Correct syntax for async error handling

Which option correctly handles errors in an async function using try-catch in Node.js?

Node.js
async function fetchData() {
  // fetch data code
}
A
async function fetchData() {
  try {
    const data = await getData();
    console.log(data);
  } catch (error) {
    console.error(error);
  }
}
B
async function fetchData() {
  const data = await getData().catch(error =&gt; console.error(error));
  console.log(data);
}
C
async function fetchData() {
  try {
    const data = getData();
    console.log(data);
  } catch (error) {
    console.error(error);
  }
}
D
async function fetchData() {
  const data = getData();
  console.log(data);
}
Attempts:
2 left
💡 Hint

Remember that await must be inside try to catch errors properly.

state_output
expert
2:00remaining
Effect of missing error handler on server state

Consider this Node.js Express server code snippet. What happens if next(error) is called but no error-handling middleware is defined?

Node.js
const express = require('express');
const app = express();

app.get('/', (req, res, next) => {
  const error = new Error('Something went wrong');
  next(error);
});

app.listen(3000);
AThe request hangs and never gets a response
BThe server ignores the error and responds with status 200
CThe server crashes because the error is unhandled
DThe server responds with a default 500 Internal Server Error page
Attempts:
2 left
💡 Hint

Express has a built-in error handler that handles errors passed to next(error) if no custom one is defined.