How to document thrown exceptions in c/.net
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In C# and .NET, exceptions thrown by a method should be documented using the <exception> XML documentation tag. This tells consumers of the API exactly which exceptions they need to handle and under what conditions they occur. Unlike Java's checked exceptions, C# does not enforce exception handling at compile time, making documentation the primary way developers communicate exception contracts. Tools like Visual Studio IntelliSense, Sandcastle, and DocFX use these tags to generate readable API documentation.
The <exception> XML Tag
Each <exception> tag specifies the exception type via cref and explains the condition under which it is thrown.
Documenting Multiple Exceptions
A method may throw several different exceptions. Document each one separately:
Interface Exception Documentation
Document exceptions on interface methods so all implementers follow the same contract:
Async Method Exceptions
For async methods, document both direct exceptions and exceptions wrapped in the returned Task:
Enabling XML Documentation Generation
To generate the XML documentation file from these tags:
The generated XML file is used by IntelliSense in Visual Studio and by documentation generators like DocFX and Sandcastle.
Common Pitfalls
- Documenting exceptions that the method does not actually throw: Only document exceptions that the method itself throws or that callers should reasonably expect from the method's direct operations. Do not document every possible exception from deep internal call chains.
- Using generic exception types in documentation: Documenting
<exception cref="Exception">provides no useful information. Document specific exception types (ArgumentNullException,IOException, etc.) so callers know exactly what to catch. - Not documenting exceptions on interface methods: If only the implementation documents exceptions, consumers who program against the interface have no visibility. Document exceptions on the interface contract.
- Forgetting that
crefis validated by the compiler: Thecrefattribute in<exception cref="...">is checked at compile time. Misspelling the exception type or missing ausingdirective produces a warning. Use the full type name or add the appropriateusingstatement. - Assuming XML docs replace proper exception handling: Documentation helps callers understand what to expect, but it does not enforce handling. Critical exception contracts should also be reinforced through clear naming, parameter validation, and unit tests that verify expected exceptions.
Summary
- Use
<exception cref="ExceptionType">XML tags to document each exception a method can throw - Explain the condition that triggers each exception in the tag body
- Document exceptions on interfaces so consumers know the contract without reading implementations
- Enable XML documentation generation in
.csprojwith<GenerateDocumentationFile>true</GenerateDocumentationFile> - Document specific exception types, not the generic
Exceptionbase class - For async methods, clarify whether exceptions are thrown immediately or via the returned Task
Related reading
- How to download a Nuget package without nuget.exe or Visual Studio extension?
- How to edit .csproj file
- How to embed a text file in a .NET assembly?
- How to empty a list in C?
- How to edit files in stopped/not starting docker container
- How to enable assembly bind failure logging (Fusion) in .NET
- How to enable assembly bind failure logging Fusion in .NET
- How to enable DataGridView sorting when user clicks on the column header?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.