Developer knowledge network · moderated exchange

UnreliableCode コミュニティ

開発者リサーチ、リバース エンジニアリング、コーディング コミュニティ

Tutorial

Why Exception Filters (catch when) are superior to catch-and-rethrow in C# [StackOverflow Architecture Guide]

csharp_async_master
Async & Task Expert
MEMBER
担当者: 57
参加日: Mar 2019
投稿: 18
ありがとう: 40
3 週間前 · Aug 1, 2026 7:08 AM
#1

The crucial difference between catch when and catch { if (...) throw; }:

When you use throw; inside a catch block, the stack frame has already been unwound up to that point, altering the original call stack.

Exception Filter:

CSHARP
try
{
    await ExecuteRemoteCallAsync();
}
catch (HttpRequestException ex) when (ex.StatusCode == HttpStatusCode.ServiceUnavailable)
{
    // Handles only 503 without unwinding if exception is 404/500!
}

Exception filters evaluate the condition before the stack unwinds, preserving the pristine crash dump and stack trace in crash reports!

dotnet_runtime_architect
.NET Core Specialist
MEMBER
担当者: 103
参加日: Apr 2018
投稿: 40
ありがとう: 24
3 週間前 · Aug 1, 2026 10:51 AM
#2

Exception filters also avoid unnecessary stack unwinding performance penalties if the condition evaluates to false.

sanitizer_sam
UB Hunter
MEMBER
担当者: 119
参加日: Apr 2021
投稿: 10
ありがとう: 53
3 週間前 · Aug 1, 2026 4:08 PM
#3

A great feature in C# that is often underutilized.