---
title: "workoutSession(_:didFailWithError:)"
framework: healthkit
role: symbol
role_heading: Instance Method
path: "healthkit/hkworkoutsessiondelegate/workoutsession(_:didfailwitherror:)"
---

# workoutSession(_:didFailWithError:)

Tells the delegate that the session failed with an error.

## Declaration

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

## Parameters

- `workoutSession`: The workout session that failed.
- `error`: An error object describing the failure.

## Discussion

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:) method. For example, if a second app starts a workout session, HealthKit calls the current session’s workoutSession(_:didFailWithError:) method. Next, it changes current session’s state to the HKWorkoutSessionState.ended value. Finally, HealthKit calls the current session’s workoutSession(_:didChangeTo:from:date:) method.

## See Also

### Tracking workout sessions

- [workoutSession(_:didChangeTo:from:date:)](healthkit/hkworkoutsessiondelegate/workoutsession(_:didchangeto:from:date:).md)
- [workoutSession(_:didGenerate:)](healthkit/hkworkoutsessiondelegate/workoutsession(_:didgenerate:).md)
- [workoutSession(_:didBeginActivityWith:date:)](healthkit/hkworkoutsessiondelegate/workoutsession(_:didbeginactivitywith:date:).md)
- [workoutSession(_:didEndActivityWith:date:)](healthkit/hkworkoutsessiondelegate/workoutsession(_:didendactivitywith:date:).md)
