Sundstrom.CheckedExceptions 1.3.1

dotnet add package Sundstrom.CheckedExceptions --version 1.3.1
                    
NuGet\Install-Package Sundstrom.CheckedExceptions -Version 1.3.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Sundstrom.CheckedExceptions" Version="1.3.1">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Sundstrom.CheckedExceptions" Version="1.3.1" />
                    
Directory.Packages.props
<PackageReference Include="Sundstrom.CheckedExceptions">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Sundstrom.CheckedExceptions --version 1.3.1
                    
#r "nuget: Sundstrom.CheckedExceptions, 1.3.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Sundstrom.CheckedExceptions@1.3.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Sundstrom.CheckedExceptions&version=1.3.1
                    
Install as a Cake Addin
#tool nuget:?package=Sundstrom.CheckedExceptions&version=1.3.1
                    
Install as a Cake Tool

CheckedExceptions for C#

Bring Java-style checked exceptions to C#: enforce handling or declaration.

NuGet License


🚀 What It Does

CheckedExceptions is a Roslyn analyzer that makes exception handling explicit.
If a method might throw an exception, the caller must either:

  • Handle it (with try/catch), or
  • Declare it (with [Throws(typeof(...))])

✅ Inspired by Java’s checked exceptions.
⚙️ Fully opt-in.
💡 Analyzer warnings by default, errors if you choose.


✅ Quick Example

public class Sample
{
    public void Execute()
    {
        // ⚠️ THROW001: Unhandled exception
        Perform();
    }

    [Throws(typeof(InvalidOperationException))]
    public void Perform()
    {
        throw new InvalidOperationException("Oops!");
    }
}

✔️ Fix it by handling:

public void Execute()
{
    try { Perform(); }
    catch (InvalidOperationException) { /* handle */ }
}

Or by declaring:

[Throws(typeof(InvalidOperationException))]
public void Execute()
{
    Perform();
}

🧠 Why Use It?

  • Avoid silent exception propagation
  • Document intent with [Throws] instead of comments
  • Enforce better error design across your codebase
  • Works with unannotated .NET methods via XML docs
  • Plays nice with nullable annotations

📦 Installation

dotnet add package Sundstrom.CheckedExceptions

And define ThrowsAttribute in your project:

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Delegate, AllowMultiple = true)]
public class ThrowsAttribute : Attribute
{
    public List<Type> ExceptionTypes { get; } = new();
    public ThrowsAttribute(Type exceptionType, params Type[] others) { … }
}

Find the full definition here.


⚙️ Configuration

.editorconfig

dotnet_diagnostic.THROW001.severity = warning
dotnet_diagnostic.THROW003.severity = warning

.csproj

<PropertyGroup>
  <WarningsAsErrors>nullable,THROW001</WarningsAsErrors>
</PropertyGroup>

JSON Settings

Add CheckedExceptions.settings.json:

{
  "ignoredExceptions": [ "System.ArgumentNullException" ],
  "informationalExceptions": {
    "System.IO.IOException": "Propagation",
    "System.TimeoutException": "Always"
  }
}

Register in .csproj:

<ItemGroup>
  <AdditionalFiles Include="CheckedExceptions.settings.json" />
</ItemGroup>

🪪 Diagnostic Codes

ID Description
THROW001 Unhandled exception: must be caught or declared
THROW003 Avoid general Exception in [Throws]
THROW004 Avoid throwing Exception directly
THROW005 Duplicate [Throws] declarations
THROW006 Declared on override, missing from base
THROW007 Declared on base, missing from override

🛠 Code Fixes

  • ✅ Add missing [Throws]
  • 🧯 Add try/catch block
  • 🪛 Suppress with #pragma or [SuppressMessage]

✨ Advanced Features

  • Supports lambdas, local functions, accessors, events
  • Analyzes exception inheritance trees
  • Merges [Throws] with <exception> from XML docs
  • Handles nullability context (#nullable enable)
  • Understands standard library exceptions (e.g. Console.WriteLineIOException)

🤝 Contributing

  1. Fork
  2. Create feature branch
  3. Push PR with tests & documentation
  4. ❤️

📜 License

MIT

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.3.1 88 6/28/2025
1.3.0 84 6/28/2025
1.2.6 111 4/12/2025
1.2.5 99 4/12/2025
1.2.4 138 4/11/2025
1.2.3 184 11/30/2024
1.2.2 105 11/29/2024
1.2.1 108 11/28/2024
1.2.0 112 11/27/2024
1.1.9 110 11/26/2024
1.1.8 111 11/26/2024
1.1.7 110 11/26/2024
1.1.6 111 11/25/2024
1.1.5 110 11/25/2024
1.1.4 117 11/25/2024
1.1.3 126 11/24/2024
1.1.2 109 11/24/2024
1.1.1 109 11/24/2024
1.1.0 116 11/23/2024
1.0.9 117 11/22/2024
1.0.8 115 11/22/2024
1.0.7 112 11/21/2024
1.0.6 115 11/21/2024
1.0.5 113 11/20/2024
1.0.4 118 11/19/2024
1.0.3 108 11/19/2024
1.0.2 119 11/19/2024
1.0.1 118 11/19/2024
1.0.0 118 11/19/2024