csharp-error-handling

Standardize C# exception handling and produce consistent API error responses.

Updated Mar 1, 2026
One-click install
npx skills add https://github.com/CloudyWing/ai-dotfiles --skill csharp-error-handling
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: csharp-error-handling
Source: https://github.com/CloudyWing/ai-dotfiles/tree/main/skills/csharp-error-handling
Command: npx skills add https://github.com/CloudyWing/ai-dotfiles --skill csharp-error-handling

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill provides concise, opinionated rules and examples to ensure C# projects use correct exception types, guard clauses, try/catch patterns, global error handling, and RFC-compliant ProblemDetails responses, reducing inconsistent error handling and fragile APIs.

Core Features & Use Cases

  • Standardize when to throw exceptions versus return values and promote guard clauses at method entry to avoid nested control flow.
  • Recommend specific exception types, best practices for custom exceptions, proper rethrowing, and forbidding empty catch blocks.
  • Guide ASP.NET Core global error handling with IExceptionHandler, ProblemDetails serialization, handler chains, and async/cancellation handling.
  • Common uses: code reviews, API refactors, implementing consistent error-response layers, and adopting the Result Pattern in services.

Quick Start

Review this ASP.NET Core endpoint and refactor its exception handling to use guard clauses, correct exception types, proper rethrowing, and standardized ProblemDetails responses.

Frequently Asked Questions about csharp-error-handling

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I standardize ASP.NET Core API error responses with ProblemDetails?

Standardize ASP.NET Core API error responses by implementing global IExceptionHandler registration and ProblemDetails serialization to ensure consistent, RFC 9457 compliant API error outputs.

When should I use guard clauses versus throwing exceptions in C#?

Use guard clauses at method entry to validate arguments and avoid nested control flow, throwing specific C# exception types only when necessary. This approach standardizes validation and prevents deep if-else nesting.

What is the best way to refactor try-catch blocks for C# exception handling?

Refactor C# exception handling by applying safe rethrowing practices, forbidding empty catch blocks, and using correct custom exception types. This ensures proper try-catch discipline during code reviews and API refactoring scenarios.

Can I use the Result Pattern instead of exceptions in my C# service layer?

Yes, you can adopt the Result Pattern in C# services to standardize when to return values instead of throwing exceptions. This reduces inconsistent error handling and fragile APIs by explicitly managing expected failures.

How do I handle async exceptions and cancellation in ASP.NET Core global handlers?

Handle async exceptions and cancellation in ASP.NET Core by applying async best practices within your global IExceptionHandler. This ensures proper cancellation token propagation and safe exception handling during asynchronous API operations.