<!--
{
  "availability" : [
    "iOS: 17.0.0 -",
    "iPadOS: 17.0.0 -",
    "macCatalyst: 17.0.0 -",
    "macOS: -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "HealthKit",
  "identifier" : "/documentation/HealthKit/HKWorkoutSessionDelegate/workoutSession(_:didFailWithError:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "HealthKit"
    ],
    "preciseIdentifier" : "c:objc(pl)HKWorkoutSessionDelegate(im)workoutSession:didFailWithError:"
  },
  "title" : "workoutSession(_:didFailWithError:)"
}
-->

# workoutSession(_:didFailWithError:)

Tells the delegate that the session failed with an error.

```
func workoutSession(_ workoutSession: HKWorkoutSession, didFailWithError error: any Error)
```

## Parameters

`workoutSession`

The workout session that failed.

`error`

An error object describing the failure.

## Discussion

When the state of the workout session changes due to an error, HealthKit always calls this method before calling the [`workoutSession(_:didChangeTo:from:date:)`](/documentation/HealthKit/HKWorkoutSessionDelegate/workoutSession(_:didChangeTo:from:date:)) method.

For example, if a second app starts a workout session, HealthKit calls the current session’s [`workoutSession(_:didFailWithError:)`](/documentation/HealthKit/HKWorkoutSessionDelegate/workoutSession(_:didFailWithError:)) method. Next, it changes current session’s state to the [`HKWorkoutSessionState.ended`](/documentation/HealthKit/HKWorkoutSessionState/ended) value. Finally, HealthKit calls the current session’s [`workoutSession(_:didChangeTo:from:date:)`](/documentation/HealthKit/HKWorkoutSessionDelegate/workoutSession(_:didChangeTo:from:date:)) method.

---

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)