0
0
Node.jsframework~5 mins

Custom error classes in Node.js - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a custom error class in Node.js?
A custom error class is a user-defined class that extends the built-in Error class to create specific error types with custom messages or properties.
Click to reveal answer
beginner
Why should you extend the built-in Error class when creating custom errors?
Extending the built-in Error class ensures your custom error behaves like a standard error, including stack traces and compatibility with error handling mechanisms.
Click to reveal answer
beginner
How do you set a custom error message in a custom error class?
You pass the message to the super() call inside the constructor of your custom error class, which sets the message property on the error instance.
Click to reveal answer
intermediate
What is the purpose of setting the name property in a custom error class?
Setting the name property helps identify the error type when logging or handling errors, making it easier to distinguish between different custom errors.
Click to reveal answer
beginner
Show a simple example of a custom error class in Node.js.
class ValidationError extends Error {
  constructor(message) {
    super(message);
    this.name = 'ValidationError';
  }
}

// Usage:
throw new ValidationError('Invalid input');
Click to reveal answer
What must a custom error class in Node.js extend to behave like a standard error?
AError
BObject
CFunction
DArray
Which method is used to pass the error message in a custom error class constructor?
Asuper(message)
Bthis.message = message
CError(message)
DsetMessage(message)
Why is setting the 'name' property in a custom error class useful?
ATo change the error message
BTo improve performance
CTo fix syntax errors
DTo identify the error type
What will happen if you throw a custom error without extending Error?
AIt behaves exactly like a standard error
BIt won't have a stack trace
CIt will crash Node.js immediately
DIt will automatically extend Error
Which of these is a correct way to throw a custom error?
Athrow Error('ValidationError')
Bthrow ValidationError('Invalid input')
Cthrow new ValidationError('Invalid input')
Dthrow new Error('ValidationError')
Explain how to create and use a custom error class in Node.js.
Think about inheritance and how errors carry messages and names.
You got /5 concepts.
    Why is it important to extend the built-in Error class when making custom errors?
    Consider what makes errors useful when debugging and handling.
    You got /4 concepts.