<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/c-tip-exception-handling-with-when-clause-whgynoupz" -->

---
title: C# Tip: Exception handling with WHEN clause | daily.dev
description: The 'when' keyword in C# (available since C# 6) lets developers add a condition to a catch block, routing exceptions to different handlers based on exception...
canonical: https://daily.dev/posts/c-tip-exception-handling-with-when-clause-whgynoupz
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: C# Tip: Exception handling with WHEN clause | daily.dev
og:description: The 'when' keyword in C# (available since C# 6) lets developers add a condition to a catch block, routing exceptions to different handlers based on exception...
og:url: https://daily.dev/posts/c-tip-exception-handling-with-when-clause-whgynoupz
og:image: https://api.daily.dev/og/posts/WhGYnOupZ.png
og:image:alt: C# Tip: Exception handling with WHEN clause
og:image:width: 1200
og:image:height: 630
og:locale: 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.

# C# Tip: Exception handling with WHEN clause

**[Code4IT](https://daily.dev/sources/code4it)** · 3 min read · 0 upvotes · 0 comments

## Summary

The 'when' keyword in C# (available since C# 6) lets developers add a condition to a catch block, routing exceptions to different handlers based on exception properties instead of using if-else inside a single catch block. Examples include a custom exception with a random value and a real-world case handling specific HTTP status codes from HttpRequestException, keeping catch blocks clean and focused.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.code4it.dev/csharptips/exception-handling-when-clause>

## Questions this post answers

### How do I catch an exception only when a specific property matches a condition in C#?

Use the 'when' keyword after the catch clause, for example catch (HttpRequestException ex) when (ex.StatusCode == (HttpStatusCode)506). This filters which catch block handles the exception based on a boolean condition, letting you write separate, cleaner catch blocks for different cases instead of nesting if-else logic inside one catch block. Available since C# 6.

_Developers refining error-handling patterns in C# can browse related techniques and examples on daily.dev._

## Similar posts on daily.dev

- [.NET Developer, Personal Blog](https://daily.dev/posts/net-developer-personal-blog-zxaphss3e) · Laszlo Deak · 13 upvotes · 0 comments
- [Cleaner switch expressions with pattern matching in C\#](https://daily.dev/posts/cleaner-switch-expressions-with-pattern-matching-in-c--oormobvwz) · The Art of Simplicity · 24 upvotes · 0 comments

---

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

[View this post on daily.dev](https://daily.dev/posts/c-tip-exception-handling-with-when-clause-whgynoupz)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"C# Tip: Exception handling with WHEN clause","url":"https://daily.dev/posts/c-tip-exception-handling-with-when-clause-whgynoupz","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/c-tip-exception-handling-with-when-clause-whgynoupz"},"datePublished":"2026-10-04T13:07:09.889Z","dateModified":"2026-10-04T13:16:37.700Z","description":"The 'when' keyword in C# (available since C# 6) lets developers add a condition to a catch block, routing exceptions to different handlers based on exception...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/2e04d44ef34cd1e1fd2ae42fb56565f5?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/2e04d44ef34cd1e1fd2ae42fb56565f5?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Code4IT","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Code4IT","logo":"https://media.daily.dev/image/upload/s--OaaG4G10--/f_auto,q_auto/v1791119049/logos/code4it?_a=BAMAMicg0","url":"https://daily.dev/sources/code4it"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/c-tip-exception-handling-with-when-clause-whgynoupz","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":".net,c#","timeRequired":"PT3M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Code4IT","item":"https://daily.dev/sources/code4it"},{"@type":"ListItem","position":3,"name":"C# Tip: Exception handling with WHEN clause"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/c-tip-exception-handling-with-when-clause-whgynoupz#faq","mainEntity":[{"@type":"Question","name":"How do I catch an exception only when a specific property matches a condition in C#?","acceptedAnswer":{"@type":"Answer","text":"Use the 'when' keyword after the catch clause, for example catch (HttpRequestException ex) when (ex.StatusCode == (HttpStatusCode)506). This filters which catch block handles the exception based on a boolean condition, letting you write separate, cleaner catch blocks for different cases instead of nesting if-else logic inside one catch block. Available since C# 6. Developers refining error-handling patterns in C# can browse related techniques and examples on daily.dev."}}]}
```

