yoklainterview sim

Backend Nodejs Error Handling Interview Questions

75 verified Backend Nodejs Error Handling interview questions — solve with answers, learn from explanations, test yourself in a real simulation.

Try the real simulation →

Sample questions

Nodejs Error HandlingDifficulty 1
In Node.js, what is the primary purpose of a try/catch block?
  • aTo register a callback that runs after all pending I/O operations finish
  • bTo automatically retry a failed function call up to three times
  • cTo catch and handle errors thrown synchronously inside the try block
  • dTo pause execution until a Promise settles, without using await
Explanation:try/catch catches exceptions thrown synchronously during the execution of the code inside the try block, letting you handle them in the catch block instead of letting them propagate and crash the program. It has no special retry or I/O-scheduling behavior.
Nodejs Error HandlingDifficulty 1
try {
  throw new Error('something failed');
} catch (e) {
  console.log(e.message);
}

What does this print?
  • a"something failed"
  • b"Error: something failed"
  • c"undefined"
  • dThe program crashes before printing anything
Explanation:e.message holds just the message text passed to the Error constructor ('something failed'). Printing e directly or calling e.toString() would include the 'Error: ' prefix, but e.message alone does not.
Nodejs Error HandlingDifficulty 2
You're writing a callback for an async file operation using the classic Node.js callback convention. What should the first parameter of that callback represent?
  • aThe number of bytes processed so far
  • bThe error (or null if none occurred), reserved as the first argument
  • cA boolean flag indicating whether the operation is complete
  • dThe result data, since Node always resolves errors internally
Explanation:Node's error-first callback convention reserves the first parameter for an error object (or null when there was no error); the actual result is passed as the second parameter, e.g. (err, data) => {...}. This convention lets every callback-based API be checked for failure the same way.
Nodejs Error HandlingDifficulty 2
const fs = require('fs');

fs.readFile('data.txt', 'utf8', (err, contents) => {
  if (err) {
    console.error('failed to read file:', err.message);
    return;
  }
  console.log(contents.toUpperCase());
});

Suppose data.txt does not exist. What happens?
  • acontents.toUpperCase() throws a TypeError that crashes the process
  • bNode retries reading the file automatically before giving up
  • cerr is null and contents is an empty string
  • dThe error branch runs, logs the failure, and returns before touching contents
Explanation:fs.readFile calls back with err set (e.g. an ENOENT error) and contents undefined when the file is missing. Because the handler checks if (err) first and returns, it never reaches contents.toUpperCase(), avoiding a crash.
Nodejs Error HandlingDifficulty 2
function parseAge(input) {
  throw new Error(`invalid age: ${input}`);
}

console.log('start');
parseAge('abc');
console.log('end');

What happens when this script runs?
  • aIt prints "start", then the process crashes with the error and never prints "end"
  • bIt prints "start", then "end", then the error is logged
  • cIt prints "start" and "end", ignoring the thrown error
  • dIt fails to compile because parseAge never returns a value
Explanation:There is no try/catch anywhere in the call chain, so the thrown Error propagates all the way up, Node prints the stack trace, and the process exits with a non-zero code. console.log('end') never runs because execution stops at the throw.
Nodejs Error HandlingDifficulty 1
Which three properties does a standard JavaScript Error object always have?
  • acode, errno, and syscall
  • bstatus, headers, and body
  • cmessage, name, and stack
  • dtype, value, and cause
Explanation:Every Error instance has message (the description you passed in), name (defaults to 'Error', overridden by subclasses), and stack (a string with the trace). code/errno/syscall are extra properties Node adds to certain system errors like fs errors, not part of the base Error shape.

Test yourself against the 3300-question Backend bank.

Start interview