0
0
Node.jsframework~20 mins

Custom error classes in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Error Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this custom error class code?

Consider this Node.js code defining a custom error class and using it:

class MyError extends Error {
  constructor(message) {
    super(message);
    this.name = 'MyError';
  }
}

try {
  throw new MyError('Oops!');
} catch (e) {
  console.log(e.name + ': ' + e.message);
}

What will be printed to the console?

Node.js
class MyError extends Error {
  constructor(message) {
    super(message);
    this.name = 'MyError';
  }
}

try {
  throw new MyError('Oops!');
} catch (e) {
  console.log(e.name + ': ' + e.message);
}
ATypeError: Oops!
BError: Oops!
CMyError: Oops!
DReferenceError: Oops!
Attempts:
2 left
💡 Hint

Look at how the name property is set in the constructor.

component_behavior
intermediate
2:00remaining
What happens when throwing a custom error without calling super()?

Examine this code snippet:

class BadError extends Error {
  constructor(message) {
    this.message = message;
    this.name = 'BadError';
  }
}

try {
  throw new BadError('Fail');
} catch (e) {
  console.log(e instanceof Error);
}

What will be logged to the console?

Node.js
class BadError extends Error {
  constructor(message) {
    this.message = message;
    this.name = 'BadError';
  }
}

try {
  throw new BadError('Fail');
} catch (e) {
  console.log(e instanceof Error);
}
AReferenceError
Btrue
CSyntaxError
Dfalse
Attempts:
2 left
💡 Hint

What happens when you access this without calling super() first?

📝 Syntax
advanced
2:00remaining
Which option correctly defines a custom error class with a stack trace?

Choose the code that correctly creates a custom error class preserving the stack trace in Node.js:

A
class CustomError extends Error {
  constructor(msg) {
    super(msg);
    this.name = 'CustomError';
  }
  captureStack() {
    Error.captureStackTrace(this, this.constructor);
  }
}
B
class CustomError extends Error {
  constructor(msg) {
    super(msg);
    this.name = 'CustomError';
    Error.captureStackTrace(this, CustomError);
  }
}
C
class CustomError extends Error {
  constructor(msg) {
    super(msg);
    this.name = 'CustomError';
    this.stack = new Error().stack;
  }
}
D
class CustomError extends Error {
  constructor(msg) {
    this.message = msg;
    this.name = 'CustomError';
    Error.captureStackTrace(this, CustomError);
  }
}
Attempts:
2 left
💡 Hint

Look for the standard way to capture stack trace in Node.js custom errors.

🔧 Debug
advanced
2:00remaining
Why does this custom error not show the correct name when logged?

Look at this code:

class MyError extends Error {
  constructor(message) {
    super(message);
  }
}

const err = new MyError('Problem');
console.log(err.name);

What will be printed and why?

Node.js
class MyError extends Error {
  constructor(message) {
    super(message);
  }
}

const err = new MyError('Problem');
console.log(err.name);
AError - because the name property was not set explicitly
BMyError - because the class name is used automatically
Cundefined - because name is not set in Error
DProblem - because message is printed instead of name
Attempts:
2 left
💡 Hint

Check if the name property is set in the constructor.

🧠 Conceptual
expert
2:00remaining
What is the main benefit of using custom error classes in Node.js?

Why should developers create custom error classes instead of using the built-in Error class directly?

ATo avoid using try-catch blocks in asynchronous code
BTo make errors run faster in Node.js runtime
CTo automatically fix bugs in the code without manual intervention
DTo add specific error names and properties for better error identification and handling
Attempts:
2 left
💡 Hint

Think about how custom errors help in debugging and managing errors.