<!--
{
  "availability" : [
    "iOS: 9.0.0 -",
    "iPadOS: 9.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.11.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSCoder/failWithError(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSCoder(im)failWithError:"
  },
  "title" : "failWithError(_:)"
}
-->

# failWithError(_:)

Signals to this coder that the decode operation has failed.

```
func failWithError(_ error: any Error)
```

## Parameters

`error`

An error that indicates why decoding failed.

## Discussion

Typically, you call this method in your [`init(coder:)`](/documentation/Foundation/NSCoding/init(coder:)) implementation. You should set the error when you detect problems such as lack of secure coding, data corruption, or a domain validation failure.

This method is only meaningful to call for decodes.

The effect of calling this method depends on the value of [`decodingFailurePolicy`](/documentation/Foundation/NSCoder/decodingFailurePolicy-swift.property), as follows:

- If the policy is [`NSCoder.DecodingFailurePolicy.raiseException`](/documentation/Foundation/NSCoder/DecodingFailurePolicy-swift.enum/raiseException), calling this method throws an exception immediately. Swift code cannot catch this kind of exception.
- If the policy is [`NSCoder.DecodingFailurePolicy.setErrorAndReturn`](/documentation/Foundation/NSCoder/DecodingFailurePolicy-swift.enum/setErrorAndReturn), calling this method sets the error property once per call to one of the `decode` methods. Calling it repeatedly has no effect until the call stack unwinds to one of these methods’ entry points.

---

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)