What is the purpose of the @Catch decorator in NestJS?
The @Catch decorator is used to define an exception filter that handles specific exceptions thrown in the application, allowing centralized error handling.
Click to reveal answer
beginner
How do you specify which exceptions a filter should catch using the @Catch decorator?
You pass one or more exception classes as arguments to the @Catch decorator. The filter will only handle those exceptions.
Click to reveal answer
intermediate
What method must be implemented in a class decorated with <code>@Catch</code> to handle exceptions?
The class must implement the <code>catch(exception: any, host: ArgumentsHost)</code> method, which contains the logic to handle the caught exception.
Click to reveal answer
intermediate
Can the @Catch decorator be used without arguments? What does that mean?
Yes, if used without arguments, the filter will catch all exceptions, acting as a global catch-all error handler.
Click to reveal answer
intermediate
How do you apply a class with the <code>@Catch</code> decorator globally in a NestJS application?
You provide the filter class in the <code>app.useGlobalFilters(new YourFilter())</code> method inside the main bootstrap function.
Click to reveal answer
What does the @Catch(HttpException) decorator do?
ACatches all exceptions
BDoes nothing without a catch method
CCatches exceptions except HttpException
DCatches only HttpException errors
✗ Incorrect
The decorator catches only exceptions of type HttpException.
Which method handles the exception inside a class decorated with @Catch?
Acatch()
Bhandle()
Cprocess()
Dexecute()
✗ Incorrect
The catch() method is required to handle exceptions.
If you want to catch all exceptions, how do you use the @Catch decorator?
A<code>@Catch(null)</code>
B<code>@Catch(AllExceptions)</code>
C<code>@Catch()</code> with no arguments
D<code>@Catch(Error)</code>
✗ Incorrect
Using @Catch() without arguments catches all exceptions.
How do you register a catch filter globally in NestJS?
AUse <code>app.useGlobalFilters()</code>
BAdd it to <code>providers</code> in a module
CUse <code>app.use()</code>
DAdd it to <code>imports</code> in a module
✗ Incorrect
Global filters are registered with app.useGlobalFilters().
What argument does the catch() method receive besides the exception?
ARequest object
BArgumentsHost object
CResponse object
DNext function
✗ Incorrect
The catch() method receives the exception and an ArgumentsHost to access request/response.
Explain how the @Catch decorator works in NestJS and how you create a custom exception filter.
Think about how you tell NestJS which errors to handle and where you write the handling code.
You got /4 concepts.
Describe the steps to apply a custom exception filter globally in a NestJS application.
Focus on how to make the filter work for the whole app, not just one controller.
You got /4 concepts.
Practice
(1/5)
1. What is the primary purpose of the @Catch() decorator in NestJS?
easy
A. To mark a class as an exception filter that handles errors
B. To define a route handler for HTTP requests
C. To inject dependencies into a service
D. To create a middleware function
Solution
Step 1: Understand the role of @Catch()
The @Catch() decorator is used to mark a class as an exception filter in NestJS, which means it handles errors thrown in the application.
Step 2: Differentiate from other decorators
Other decorators like route handlers or dependency injections serve different purposes, so @Catch() specifically relates to error handling.
Final Answer:
To mark a class as an exception filter that handles errors -> Option A
Quick Check:
Error handling = @Catch() [OK]
Hint: Catch decorator marks error handlers in NestJS classes [OK]
Common Mistakes:
Confusing @Catch() with route or middleware decorators
Thinking @Catch() injects dependencies
Assuming @Catch() handles normal requests
2. Which of the following is the correct syntax to create an exception filter that catches all exceptions using the @Catch() decorator?
easy
A. @Catch()\nexport class AllExceptionsFilter implements ExceptionFilter { ... }
B. @Catch(Error)\nexport class AllExceptionsFilter implements ExceptionFilter { ... }
C. @Catch(HttpException)\nexport class AllExceptionsFilter implements ExceptionFilter { ... }
D. @Catch(Exception)\nexport class AllExceptionsFilter implements ExceptionFilter { ... }
Solution
Step 1: Understand @Catch() usage without parameters
Using @Catch() with no arguments means the filter will catch all exceptions.
Step 2: Check other options
Options A, C, and D specify particular exception types, so they do not catch all exceptions.
Final Answer:
@Catch()\nexport class AllExceptionsFilter implements ExceptionFilter { ... } -> Option A
Quick Check:
No argument = catch all exceptions [OK]
Hint: Empty @Catch() catches all exceptions [OK]
Common Mistakes:
Passing incorrect exception types to @Catch()
Using @Catch() with a non-error class
Confusing @Catch() with middleware syntax
3. Given the following exception filter code, what will be the output when a NotFoundException is thrown?
B. The exception parameter lacks a type annotation
C. The catch method should return a value
D. The @Catch() decorator must specify an exception type
Solution
Step 1: Check method signature
The catch method's first parameter exception should have a type annotation, typically unknown or any.
Step 2: Verify other parts
The @Catch() decorator can be empty to catch all exceptions, returning void is allowed, and response retrieval is correct.
Final Answer:
The exception parameter lacks a type annotation -> Option B
Quick Check:
Missing parameter type = The exception parameter lacks a type annotation [OK]
Hint: Always type the exception parameter in catch method [OK]
Common Mistakes:
Forgetting to type the exception parameter
Thinking @Catch() requires arguments
Expecting catch to return a value
5. You want to create a single exception filter that handles both BadRequestException and UnauthorizedException with different custom messages. Which is the correct way to use the @Catch() decorator to achieve this?
hard
A. @Catch()\nexport class MultiExceptionFilter implements ExceptionFilter { ... }
B. @Catch([BadRequestException, UnauthorizedException])\nexport class MultiExceptionFilter implements ExceptionFilter { ... }
C. @Catch(BadRequestException, UnauthorizedException)\nexport class MultiExceptionFilter implements ExceptionFilter { ... }
D. Create two separate filters each with @Catch() for one exception
Solution
Step 1: Understand how to catch multiple exceptions
The @Catch() decorator accepts multiple exception classes as separate arguments to catch them in one filter.
Step 2: Evaluate options
@Catch(BadRequestException, UnauthorizedException)\nexport class MultiExceptionFilter implements ExceptionFilter { ... } correctly passes multiple exceptions as separate arguments. @Catch([BadRequestException, UnauthorizedException])\nexport class MultiExceptionFilter implements ExceptionFilter { ... } incorrectly passes an array. @Catch()\nexport class MultiExceptionFilter implements ExceptionFilter { ... } catches all exceptions. Create two separate filters each with @Catch() for one exception uses multiple filters, not one.
Final Answer:
@Catch(BadRequestException, UnauthorizedException)\nexport class MultiExceptionFilter implements ExceptionFilter { ... } -> Option C
Quick Check:
Multiple exceptions as arguments = @Catch(BadRequestException, UnauthorizedException)\nexport class MultiExceptionFilter implements ExceptionFilter { ... } [OK]
Hint: Pass multiple exceptions as separate arguments to @Catch() [OK]
Common Mistakes:
Passing exceptions as an array to @Catch()
Using multiple filters instead of one
Using empty @Catch() to catch specific exceptions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.