Bird
Raised Fist0
Node.jsframework~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

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
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.

Practice

(1/5)
1. What is the main reason to create a custom error class in Node.js?
easy
A. To define specific error types for clearer error handling
B. To make the program run faster
C. To avoid using try-catch blocks
D. To replace the built-in Error class completely

Solution

  1. Step 1: Understand the purpose of custom errors

    Custom error classes help identify and handle specific error cases clearly in your code.
  2. Step 2: Compare with other options

    Custom errors do not speed up code, avoid try-catch, or replace Error class but extend it.
  3. Final Answer:

    To define specific error types for clearer error handling -> Option A
  4. Quick Check:

    Custom error purpose = Specific error types [OK]
Hint: Custom errors clarify error types, not speed or structure [OK]
Common Mistakes:
  • Thinking custom errors improve performance
  • Believing custom errors remove need for try-catch
  • Assuming custom errors replace built-in Error
2. Which of the following is the correct way to define a custom error class named MyError in Node.js?
easy
A. class MyError extends Object { constructor(message) { super(message); this.name = 'MyError'; } }
B. function MyError() { this.message = 'Error'; this.name = 'MyError'; }
C. class MyError { constructor(message) { this.message = message; } }
D. class MyError extends Error { constructor(message) { super(message); this.name = 'MyError'; } }

Solution

  1. Step 1: Check class inheritance

    Custom errors must extend the built-in Error class to behave like errors.
  2. Step 2: Verify constructor and name setting

    The constructor calls super(message) and sets this.name to the class name for clarity.
  3. Final Answer:

    class MyError extends Error { constructor(message) { super(message); this.name = 'MyError'; } } -> Option D
  4. Quick Check:

    Extend Error and set name = MyError [OK]
Hint: Extend Error and set this.name in constructor [OK]
Common Mistakes:
  • Not extending Error class
  • Forgetting to call super(message)
  • Not setting the error name property
3. What will be the output of this code snippet?
class NotFoundError extends Error {
  constructor(message) {
    super(message);
    this.name = 'NotFoundError';
  }
}

try {
  throw new NotFoundError('Item not found');
} catch (e) {
  console.log(e.name + ': ' + e.message);
}
medium
A. Error: Item not found
B. NotFoundError: Item not found
C. TypeError: Item not found
D. undefined: Item not found

Solution

  1. Step 1: Understand custom error name

    The custom error sets this.name = 'NotFoundError', so e.name is 'NotFoundError'.
  2. Step 2: Check the output format

    The console.log prints e.name + ': ' + e.message, which becomes 'NotFoundError: Item not found'.
  3. Final Answer:

    NotFoundError: Item not found -> Option B
  4. Quick Check:

    Custom error name shows in output [OK]
Hint: Custom error name property appears in error output [OK]
Common Mistakes:
  • Assuming default 'Error' name instead of custom
  • Confusing error type with TypeError
  • Missing the custom name property effect
4. Identify the error in this custom error class definition:
class ValidationError extends Error {
  constructor(msg) {
    this.message = msg;
    this.name = 'ValidationError';
  }
}
medium
A. Missing call to super() in constructor
B. Incorrect class name
C. Should extend Object instead of Error
D. No error, code is correct

Solution

  1. Step 1: Check constructor for super call

    When extending Error, constructor must call super() before using this.
  2. Step 2: Identify missing super call

    The code sets this.message and this.name without calling super(msg), causing a runtime error.
  3. Final Answer:

    Missing call to super() in constructor -> Option A
  4. Quick Check:

    Always call super() first in subclass constructor [OK]
Hint: Always call super() before using this in constructor [OK]
Common Mistakes:
  • Forgetting super() call causes ReferenceError
  • Setting this.message before super()
  • Extending wrong base class
5. You want to create a custom error class AuthError that includes a statusCode property for HTTP status codes. Which implementation correctly adds this property and preserves the error behavior?
hard
A. class AuthError extends Error { constructor(statusCode) { super(); this.name = 'AuthError'; this.statusCode = statusCode; } }
B. class AuthError extends Error { constructor(message, statusCode) { this.message = message; this.statusCode = statusCode; this.name = 'AuthError'; } }
C. class AuthError extends Error { constructor(message, statusCode) { super(message); this.name = 'AuthError'; this.statusCode = statusCode; } }
D. class AuthError { constructor(message, statusCode) { this.message = message; this.statusCode = statusCode; this.name = 'AuthError'; } }

Solution

  1. Step 1: Check proper Error extension and constructor

    The class must extend Error and call super(message) to set the error message correctly.
  2. Step 2: Verify additional property and name

    Setting this.statusCode after super() adds the extra info; setting this.name clarifies error type.
  3. Final Answer:

    class AuthError extends Error { constructor(message, statusCode) { super(message); this.name = 'AuthError'; this.statusCode = statusCode; } } -> Option C
  4. Quick Check:

    Extend Error, call super(message), add extra properties [OK]
Hint: Call super(message) first, then add extra properties [OK]
Common Mistakes:
  • Not calling super(message) in constructor
  • Not extending Error class
  • Missing message parameter in super call