dotnet / dotnet/roslyn

Add Exception Handling Robustness Analysis Rules to Roslyn

Open
#81,371 0 comments 0 reactions 0 assignees View on GitHub
Area-Analyzers Area-IDE Feature Request
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

**Brief description:**
In current C# development practices, exception handling often relies on developer discipline and team conventions, lacking enforcement from the compiler and static analysis tools. This frequently leads to critical production failures due to unhandled exceptions.

The recent Cloudflare global service outage serves as a prime example. Their [Rust component panicked due to `unwrap()` usage](https://blog.cloudflare.com/18-november-2025-outage/), rendering core services unavailable. While C# uses a different error handling model, similar issues exist: developers may throw exceptions without proper documentation, and callers may fail to handle known exception scenarios.

Propose a set of code style analyzers to enforce robust exception handling practices by requiring proper documentation of thrown exceptions and validation of exception handling at call sites. These rules aim to prevent production failures like the recent Cloudflare incident caused by inadequate error handling.

**Languages applicable:**

c#

**Code example that the analyzer should report:**

Rule EX001: Missing exception documentation for public method with throw statements

```csharp

// ❌ Violates rule - throws exception but not documented
public string LoadConfiguration(string path)
{
if (!File.Exists(path))
throw new FileNotFoundException(); // EX001: Missing exception documentation

return File.ReadAllText(path);
}

// ✅ Compliant
/// Loads configuration file
/// Thrown when configuration file doesn't exist
public string LoadConfiguration(string path)
{
if (!File.Exists(path))
throw new FileNotFoundException();

return File.ReadAllText(path);
}
```

Rule EX002: Require that when calling methods with documented exceptions, the caller must explicitly handle these exceptions.
```csharp
// ❌ Violates rule - doesn't handle documented exceptions
public void InitializeService()
{
var config = LoadConfiguration("app.config"); // EX003: Unhandled documented exception
// If LoadConfiguration throws FileNotFoundException, this will crash
}

// ✅ Compliant
public void InitializeService()
{
try
{
var config = LoadConfiguration("app.config");
}
catch (FileNotFoundException ex)
{
// Provide fallback
UseDefaultConfiguration();
Logger.Warning(ex, "Config file missing, using defaults");
}
catch (UnauthorizedAccessException ex)
{
// Or rethrow
throw new ServiceInitializationException("Cannot access config file", ex);
}
}
```

**Additional information:**

Expected Benefits
- Improved Code Reliability: Enforces explicit exception handling, reducing production crashes

- Better Developer Experience: Catches exception handling issues at compile time rather than runtime

- Promotes API Design: Encourages designing APIs with clear error contracts

- Facilitates Code Review: Makes exception handling a verifiable code quality metric

The Cloudflare incident reminds us that modern software systems require stronger error handling guarantees. By adding exception handling analysis rules to Roslyn, we can significantly improve the robustness of C# applications while maintaining the language's flexibility and development efficiency.

These rules would help developers establish proper exception handling habits during the coding phase, preventing recurrences of incidents like Cloudflare's.

**Documentation requirements:**

Contributor guide

Open the contributing guide

Research direction

No implementation files, tests, or entry points are named. Start with GuidelinesForNewRules.md and evaluate the proposed EX001 and EX002 rules against Roslyn's analyzer model. Done would require a decided, scoped rule design with diagnostics and documented behavior for the examples and exception-handling cases described.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.