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
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
✗ Incorrect
Custom error classes should extend the built-in Error class to inherit standard error behavior.
Which method is used to pass the error message in a custom error class constructor?
Asuper(message)
Bthis.message = message
CError(message)
DsetMessage(message)
✗ Incorrect
The super(message) call passes the message to the base Error class constructor.
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
✗ Incorrect
The 'name' property helps identify the error type when handling or logging errors.
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
✗ Incorrect
Without extending Error, the custom error won't have a proper stack trace or behave like a standard 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')
✗ Incorrect
You must create an instance of the custom error class using 'new' before throwing it.
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.
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
Step 1: Understand the purpose of custom errors
Custom error classes help identify and handle specific error cases clearly in your code.
Step 2: Compare with other options
Custom errors do not speed up code, avoid try-catch, or replace Error class but extend it.
Final Answer:
To define specific error types for clearer error handling -> Option A
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
Step 1: Check class inheritance
Custom errors must extend the built-in Error class to behave like errors.
Step 2: Verify constructor and name setting
The constructor calls super(message) and sets this.name to the class name for clarity.
Final Answer:
class MyError extends Error { constructor(message) { super(message); this.name = 'MyError'; } } -> Option D
Quick Check:
Extend Error and set name = MyError [OK]
Hint: Extend Error and set this.name in constructor [OK]
When extending Error, constructor must call super() before using this.
Step 2: Identify missing super call
The code sets this.message and this.name without calling super(msg), causing a runtime error.
Final Answer:
Missing call to super() in constructor -> Option A
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
Step 1: Check proper Error extension and constructor
The class must extend Error and call super(message) to set the error message correctly.
Step 2: Verify additional property and name
Setting this.statusCode after super() adds the extra info; setting this.name clarifies error type.
Final Answer:
class AuthError extends Error {
constructor(message, statusCode) {
super(message);
this.name = 'AuthError';
this.statusCode = statusCode;
}
} -> Option C
Quick Check:
Extend Error, call super(message), add extra properties [OK]
Hint: Call super(message) first, then add extra properties [OK]