Home » RxJS catchError() Error Handling Operator

RxJS catchError() Error Handling Operator

by Online Tutorials Library

RxJS catchError() Error Handling Operator

RxJS catchError() operator is an error-handling operator used to handle and take care of catching errors on the source observable by returning a new observable or an error.

In other words, we can say that the RxJS catchError() operator catches errors on the observable and gracefully handles errors in an observable sequence. It handles the errors by returning a new observable or throwing an error.

Syntax:

Following is the syntax of the RxJS catchError() error handling operator:

Or

Parameter Explanation

  • selector_funct: The selector func takes two arguments, err, the error function, and caught, which is the source observable.
  • selector: It specifies a function that takes as arguments err, which is the error, and caught, which is the source observable. In the case, you’d like to “retry” that observable by returning it. Whatever observable is returned by the selector will be used to continue the observable chain.

Return value

The RxJS catchError() operator returns an observable that originates from either the source or the observable returned by the catch selector function or selector_func.

Let us see some examples of the RxJS catchError() operator to understand it clearly.

Example 1 (Catching error from observable)

Output:

After executing the above example, you will see the following result:

RxJS catchError() Error Handling Operator

Example 1 (Observable when there is an error)

Output:

After executing the above example, you will see the following result:

RxJS catchError() Error Handling Operator

Example 2

Output:

After executing the above example, you will see the following result:

RxJS catchError() Error Handling Operator

Example 3 (Catching rejected promise)

Output:

After executing the above example, you will see the following result:

RxJS catchError() Error Handling Operator


Next TopicRxJS Operators

You may also like