Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

angular

What is error handling in Angular?

Sheza Munir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

An error arises when a program’s execution has unexpected results. There are 3 usual errors in Angular:

  1. External
  2. Internal
  3. Application

Error handling in Angular is done with the help of the handleError function. This function allows centralized exception handling and is provided to the application when the application is initialized.

Class

The handleError function is a part of the errorHandler class.

Parameter

The handleError function takes the exception raised as a parameter.

Output

The handleError function, by default, only prints error messages to the console. However, a customized exception handler can be added to intercept and modify the default behavior.

Code

The following code explains the syntax for errorHandler class. This is the default implementation that can be changed.

class MyErrorHandler implements ErrorHandler {
//this function is called when the application throws an error
handleError(error: Error) {
// handle the exception here
}
}
@NgModule({
providers: [{provide: ErrorHandler, useClass: MyErrorHandler}]
})
class MyModule {}

RELATED TAGS

angular

CONTRIBUTOR

Sheza Munir
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring