---
title: "Throw ThrowIfCancellationRequested in a Method that Returns a Value when Using a Cancellation Token"
url: https://daily.dev/posts/throw-throwifcancellationrequested-in-a-method-that-returns-a-value-when-using-a-cancellation-token-z0et7szbe
source_url: https://nodogmablog.bryanhogan.net/2024/01/throw-throwifcancellationrequested-in-a-method-that-returns-a-value-when-using-a-cancellation-token
type: article
source: "No Dogma Blog (Bryan Hogan)"
published: 2026-05-31T07:46:26.407Z
updated: 2026-05-31T09:02:16.401Z
tags: ["c#"]
reading_time: 3
upvotes: 0
comments: 0
language: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Throw ThrowIfCancellationRequested in a Method that Returns a Value when Using a Cancellation Token

**[No Dogma Blog \(Bryan Hogan\)](https://daily.dev/sources/bryanhogan)** · 3 min read · 0 upvotes · 0 comments

## Summary

When a C# async method returns a value, cancellation via CancellationToken creates a dilemma: you must either return a dummy value or throw an exception. Returning a sentinel value like -1 is misleading to readers. The cleaner alternative is calling token.ThrowIfCancellationRequested(), which throws an OperationCanceledException that callers can catch. A code example shows running two tasks with Task.WhenAny, canceling the remaining task after the first completes, and handling the resulting exception in a foreach loop.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://nodogmablog.bryanhogan.net/2024/01/throw-throwifcancellationrequested-in-a-method-that-returns-a-value-when-using-a-cancellation-token>

## Similar posts on daily.dev

- [CancellationToken in C\# – Pitfalls and Winning Patterns](https://daily.dev/posts/cancellationtoken-in-c-pitfalls-and-winning-patterns-qc515snyt) · Atomic Spin · 66 upvotes · 2 comments

---

Tags: [#c#](https://daily.dev/tags/c#)

[View this post on daily.dev](https://daily.dev/posts/throw-throwifcancellationrequested-in-a-method-that-returns-a-value-when-using-a-cancellation-token-z0et7szbe)
