CARVIEW |
Error Handling in Express.js Quiz
Question 1
What happens if you call 'next()' without an argument from a regular middleware?
It triggers the default Express error handler.
It immediately terminates the request-response cycle.
It passes control to the next regular middleware in the stack.
It passes control to the next error-handling middleware in the stack.
Question 2
How does Express handle errors that occur in asynchronous code?
It ignores them, and the request will hang indefinitely.
It automatically catches them and passes them to the error-handling middleware.
You must catch them and pass them to the 'next()' function.
It crashes the application unless a 'try...catch' block is used.
Question 3
Where should a custom error-handling middleware be placed in your Express application?
At the very beginning of the middleware stack.
In a separate file, imported and used where needed.
Immediately after the middleware that is most likely to throw an error.
After all other 'app.use()' and route calls.
Question 4
What is the purpose of 'Error.captureStackTrace(this, this.constructor)' in a custom error class?
To add a timestamp to the error's stack trace
To create a cleaner stack trace by excluding the constructor call of the custom error class
To log the error to the console automatically
To prevent the application from crashing when an error is thrown
Question 5
What is the default behavior of the built-in Express error handler in a production environment?
It sends a '500 Internal Server Error' response with a detailed stack trace.
It sends a '500 Internal Server Error' response with no error details.
It logs the error to the console and sends a generic error message to the client.
It attempts to recover from the error and continue processing the request.
Question 6
If you have multiple error-handling middleware functions, how does Express determine which one to execute?
It executes the first one that is defined in the middleware stack.
It executes all of them in the order they are defined.
It executes them in the order they are defined, until one of them does not call 'next(err)'.
It executes the one that best matches the error type.
Question 7
What is a key benefit of creating a custom 'Error' class that extends the built-in 'Error' class?
It prevents the application from crashing when an error is thrown.
It provides a more detailed stack trace than the built-in 'Error' class.
It automatically logs all errors to a file.
It automatically logs all errors to a file.
Question 8
How can you trigger an error-handling middleware from a regular route handler?
By calling 'next(new Error('Something went wrong'))'
By returning a new 'Error('Something went wrong')'
By calling 'res.status(500).send('Error')'
By throwing a new 'Error('Something went wrong')'
Question 9
What is the purpose of the 'err' parameter in an error-handling middleware function?
It is a callback function that is used to pass control to the next middleware.
It is an object containing information about the incoming HTTP request.
It is the error object that was passed to the 'next()' function.
It is an object containing information about the HTTP response.
Question 10
Which of the following is a valid way to create a custom error in Express?
next('Error', 500);
throw { message: 'Error', statusCode: 500 };
const err = { message: 'Error', statusCode: 500 }; next(err);
class CustomError extends Error { constructor(message, statusCode) { super(message); this.statusCode = statusCode; } } const err = new CustomError('Error', 500); next(err);
There are 10 questions to complete.