Angular - How to implement Exception Handling on component level Angular - How to implement Exception Handling on component level angular angular

Angular - How to implement Exception Handling on component level


I would approach it by handling the error at Component level and have a service that listens to any errors happening at Component or Service level.Ex:

  1. Throw the error from the service
  2. catch the error in component
  3. Handle the error, process it and send the Error event with details to ErrorService.
  4. You can have a app level component "errorBannerComponent" which takes input from ErrorService and paint your UI.
  5. As soon as the error is received in ErrorService, The errorBannerComponent should display the error on screen.

Hope it helps.

Also By default, Angular comes with its own ErrorHandler that intercepts all the Errors that happen in our app and logs them to the console, preventing the app from crashing. We can modify this default behavior by creating a new class that implements the ErrorHandler:

You can find more details and example here: