0
0
NestJSframework~10 mins

Catch decorator in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Catch decorator
Define Catch Decorator
Apply Decorator to Method
Method Executes
Error Occurs?
NoReturn Result
Yes
Catch Decorator Handles Error
Return Custom Error Response
The Catch decorator wraps a method to catch errors during execution and handle them gracefully.
Execution Sample
NestJS
import { Catch, ExceptionFilter, ArgumentsHost } from '@nestjs/common';

@Catch()
export class MyExceptionFilter implements ExceptionFilter {
  catch(exception: any, host: ArgumentsHost) {
    return 'Error handled';
  }
}
Defines a Catch decorator that intercepts errors thrown in a method and returns a custom message.
Execution Table
StepActionMethod ExecutionError Thrown?Catch Decorator ResponseFinal Output
1Method calledRuns normallyNoNo actionMethod result
2Method calledThrows errorYesCatch decorator interceptsReturns 'Error handled'
3Execution endsN/AN/AN/AOutput returned to caller
💡 Execution stops after method returns or catch decorator handles error
Variable Tracker
VariableStartAfter Step 1After Step 2Final
exceptionundefinedundefinedError objectError object
hostundefinedRequest contextRequest contextRequest context
method resultundefinedNormal resultundefinedError handled
Key Moments - 2 Insights
Why does the catch decorator run only when an error occurs?
Because as shown in execution_table row 2, the catch decorator intercepts only when the method throws an error; otherwise, it lets the normal result pass (row 1).
What happens if the method does not throw an error?
The catch decorator does nothing and the method returns its normal result, as seen in execution_table row 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the final output when the method throws an error?
AUndefined
BMethod result
C'Error handled'
DException object
💡 Hint
Check execution_table row 2 under 'Final Output'
At which step does the catch decorator intercept the error?
AStep 2
BStep 1
CStep 3
DNever
💡 Hint
See execution_table row 2 under 'Catch Decorator Response'
If the method never throws an error, what will the catch decorator do?
AReturn 'Error handled'
BDo nothing
CModify the method result
DThrow a new error
💡 Hint
Refer to execution_table row 1 where 'Error Thrown?' is No
Concept Snapshot
Catch decorator in NestJS:
- Wraps methods to catch thrown errors
- Executes only if an error occurs
- Returns custom error response
- Allows normal method result if no error
- Used for centralized error handling
Full Transcript
The Catch decorator in NestJS is used to handle errors thrown by methods. When a method runs, if it completes without error, the catch decorator does nothing and the method's result is returned. If the method throws an error, the catch decorator intercepts it and returns a custom response, such as an error message. This helps keep error handling clean and centralized. The execution table shows the steps: method call, error thrown or not, catch decorator response, and final output. Variables like the exception object and method result change accordingly. Understanding when the catch decorator activates is key to using it effectively.