0
0
NestJSframework~10 mins

Built-in HTTP exceptions in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Built-in HTTP exceptions
Request received
Controller method runs
Check for error condition
Yes
Throw built-in HTTP exception
NestJS catches exception
Send HTTP error response
Client receives error status and message
When an error condition occurs in a controller, NestJS built-in HTTP exceptions are thrown, caught by the framework, and converted into proper HTTP error responses sent back to the client.
Execution Sample
NestJS
import { Controller, Get, Param, NotFoundException } from '@nestjs/common';

@Controller('items')
export class ItemsController {
  @Get(':id')
  getItem(@Param('id') id: string) {
    throw new NotFoundException('Item not found');
  }
}
This code throws a 404 Not Found HTTP exception when the getItem method is called.
Execution Table
StepActionException Thrown?Exception TypeResponse Sent
1Request to GET /items/:id receivedNoN/AN/A
2Controller method getItem() executesYesNotFoundExceptionNo
3NestJS catches NotFoundExceptionYesNotFoundExceptionNo
4NestJS sends HTTP 404 response with message 'Item not found'YesNotFoundExceptionHTTP 404 Not Found with message
5Client receives HTTP 404 responseNoN/AHTTP 404 Not Found with message
💡 Exception thrown in controller, caught by NestJS, HTTP 404 response sent to client
Variable Tracker
VariableStartAfter Step 2After Step 4Final
exceptionundefinedNotFoundException instance with message 'Item not found'Handled by NestJSResponse sent to client
Key Moments - 2 Insights
Why does the controller method throw an exception instead of returning a response?
Throwing a built-in HTTP exception immediately stops the method and signals NestJS to handle the error and send the correct HTTP response, as shown in execution_table step 2 and 3.
What happens if no exception is thrown in the controller?
If no exception is thrown, NestJS sends the normal successful response. The execution_table shows no exception thrown in step 1 and 5, meaning normal flow continues.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does NestJS catch the exception?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Check the 'Exception Thrown?' and 'Exception Type' columns in the execution_table at step 3
According to the variable tracker, what is the state of the 'exception' variable after step 4?
AHandled by NestJS
BNotFoundException instance
CUndefined
DResponse sent to client
💡 Hint
Look at the 'exception' row and the 'After Step 4' column in variable_tracker
If the controller did not throw an exception, what would the execution table show at step 2?
AException Thrown? = Yes
BResponse Sent = HTTP 404
CException Thrown? = No
DException Type = NotFoundException
💡 Hint
Refer to the 'Exception Thrown?' column in execution_table step 1 and 5 for normal flow
Concept Snapshot
Built-in HTTP exceptions in NestJS are classes like NotFoundException.
Throw them in controllers to signal errors.
NestJS catches these exceptions automatically.
It sends the proper HTTP status and message to clients.
Use exceptions to simplify error handling in your app.
Full Transcript
In NestJS, when a controller detects an error, it throws a built-in HTTP exception like NotFoundException. This stops the controller method immediately. NestJS framework catches this exception and converts it into an HTTP response with the correct status code and message. For example, throwing NotFoundException sends a 404 status with a message. This flow helps developers handle errors cleanly without manually crafting responses. The execution table shows each step: request received, exception thrown, NestJS catching it, sending response, and client receiving it. The variable tracker shows the exception variable state changes. Key moments clarify why throwing exceptions is preferred and what happens if no exception is thrown. The visual quiz tests understanding of when exceptions are caught and how variables change. This method makes error handling in NestJS simple and consistent.