<!--
{
  "availability" : [
    "iOS: 13.0.0 -",
    "iPadOS: 13.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.15.0 -",
    "tvOS: 13.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 6.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Combine",
  "identifier" : "/documentation/Combine/Publisher/replaceError(with:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Combine"
    ],
    "preciseIdentifier" : "s:7Combine9PublisherPAAE12replaceError4withAA10PublishersO07ReplaceD0Vy_xG6OutputQz_tF"
  },
  "title" : "replaceError(with:)"
}
-->

# replaceError(with:)

Replaces any errors in the stream with the provided element.

```
func replaceError(with output: Self.Output) -> Publishers.ReplaceError<Self>
```

## Parameters

`output`

An element to emit when the upstream publisher fails.

## Return Value

A publisher that replaces an error from the upstream publisher with the provided output element.

## Discussion

If the upstream publisher fails with an error, this publisher emits the provided element, then finishes normally.

In the example below, a publisher of strings fails with a `MyError` instance, which sends a failure completion downstream. The [`replaceError(with:)`](/documentation/Combine/Publisher/replaceError(with:)) operator handles the failure by publishing the string `(replacement element)` and completing normally.

```
struct MyError: Error {}
let fail = Fail<String, MyError>(error: MyError())
cancellable = fail
    .replaceError(with: "(replacement element)")
    .sink(
        receiveCompletion: { print ("\($0)") },
        receiveValue: { print ("\($0)", terminator: " ") }
    )

// Prints: "(replacement element) finished".
```

This [`replaceError(with:)`](/documentation/Combine/Publisher/replaceError(with:)) functionality is useful when you want to handle an error by sending a single replacement element and end the stream. Use [`catch(_:)`](/documentation/Combine/Publisher/catch(_:)) to recover from an error and provide a replacement publisher to continue providing elements to the downstream subscriber.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)