Generic Instance Method


Handles errors from an upstream publisher by replacing it with another publisher.


func `catch`<P>(_ handler: @escaping (Upstream.Failure) -> P) -> Publishers.Catch<Publishers.Share<Upstream>, P> where P : Publisher, Self.Output == P.Output



A closure that accepts the upstream failure as input and returns a publisher to replace the upstream publisher.

Return Value

A publisher that handles errors from an upstream publisher by replacing the failed publisher with another publisher.


The following example replaces any error from the upstream publisher and replaces the upstream with a Just publisher. This continues the stream by publishing a single value and completing normally.

enum SimpleError: Error { case error }
let errorPublisher = (0..<10).publisher().tryMap { v -> Int in
    if v < 5 {
        return v
    } else {
        throw SimpleError.error

let noErrorPublisher = errorPublisher.catch { _ in
    return Publishers.Just(100)

Backpressure note: This publisher passes through request and cancel to the upstream. After receiving an error, the publisher sends sends any unfulfilled demand to the new Publisher.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software