---
title: "withTaskCancellationHandler(handler:operation:)"
framework: swift
role: symbol
role_heading: Function
path: "swift/withtaskcancellationhandler(handler:operation:)"
---

# withTaskCancellationHandler(handler:operation:)

## Declaration

```swift
func withTaskCancellationHandler<T>(handler: @Sendable () -> Void, operation: () async throws -> T) async rethrows -> T
```

## See Also

### Canceling Tasks

- [CancellationError](swift/cancellationerror.md)
- [cancel()](swift/task/cancel().md)
- [isCancelled](swift/task/iscancelled-swift.property.md)
- [isCancelled](swift/task/iscancelled-swift.type.property.md)
- [checkCancellation()](swift/task/checkcancellation().md)
- [withTaskCancellationHandler(operation:onCancel:isolation:)](swift/withtaskcancellationhandler(operation:oncancel:isolation:).md)
