Custom Exception

NestJS provides a set of built-in exceptions. These exceptions cover standard HTTP status codes, such as “404 Not Found,” “500 Internal Server Error,” etc. Sometimes, we might also need to create a custom exception for the specific needs of our application.

In this lesson, we’ll explore the built-in exceptions and learn how to implement custom exceptions to manage exceptions effectively.

The HttpException exception and other built-in exceptions

In the previous lesson, we used the built-in HttpException. The HttpException exception is handled by default in NestJS. If HttpException is thrown, it will be mapped to the appropriate HTTP response and returned to the client.

A HttpException exception takes two parameters: response message and status code. We can throw an exception using the following code:

Get hands-on with 1400+ tech skills courses.