<exception> tags (diagnostic CSENSE012). You can customize which exceptions to ignore and how deeply to scan for exceptions.
Exception Detection
By default, CommentSense scans method bodies for explicitly thrown exceptions, including:- Direct
throwstatements:throw new ArgumentException(...); - Static guard clauses:
ArgumentNullException.ThrowIfNull(parameter); - Static throw helpers:
ThrowHelper.ThrowArgumentException();
CommentSense only detects exceptions that are explicitly thrown in the method body. It does not analyze exceptions thrown by called methods unless configured to do so.
Ignoring Specific Exceptions
Comma-separated list of exception types (by name or full name) that should be ignored by CSENSE012. Values are case-insensitive.You can specify either the simple name (e.g.,
ArgumentNullException) or the full name (e.g., System.ArgumentNullException).Ignoring System Exceptions
When set to
true, ignores all exceptions in the System namespace and its sub-namespaces.This is a broad filter that excludes common framework exceptions like ArgumentException, InvalidOperationException, NotSupportedException, etc.Ignoring Exception Namespaces
Comma-separated list of namespaces. Exceptions in these namespaces (or sub-namespaces) will be ignored. Values are case-insensitive.
Namespace matching includes sub-namespaces. For example,
System.Data matches System.Data.SqlClient.SqlException.Scanning Called Methods for Exceptions
When set to
true, CSENSE012 will also analyze exceptions documented in the XML comments of methods and constructors called within the analyzed method.This provides deeper analysis but can be more aggressive, potentially requiring documentation for exceptions you may not want to expose.This setting only analyzes exceptions that are documented in called methods. It does not perform deep static analysis of the called method’s implementation.
Configuration Examples
Minimal Exception Documentation
Only document custom exceptions:Selective Exception Documentation
Ignore common validation exceptions but document others:Strict Exception Documentation
Document all exceptions including those from called methods:API-Specific Configuration
Different rules for public API vs internal code:Combining Exception Filters
Exception filters are cumulative. An exception is ignored if it matches any of the following:- Listed in
ignored_exceptions - In the
Systemnamespace (ifignore_system_exceptions = true) - In a namespace listed in
ignored_exception_namespaces
Related Rules
- CSENSE012 - Missing exception documentation
- CSENSE017 - Invalid exception type in
<exception>tag - CSENSE023 - Stray or duplicate
<exception>tags
Automatic Code Fixes
CommentSense provides automatic code fixes for exception documentation:Generate Missing Exception Tag
CSENSE012 offers a code fix to generate a missing
<exception> tag with a placeholder. Supports Fix All in document, project, or solution.Rename Misspelled Exception
If a fuzzy match is found among existing exception tags, a code fix to rename the tag is offered based on
rename_similarity_threshold.Remove Stray Exception Tags
CSENSE023 offers a code fix to remove stray or duplicate exception tags. Supports Fix All in document, project, or solution.
Best Practices
- Start conservatively - Use
ignore_system_exceptions = trueinitially to focus on custom exceptions - Be selective - Use
ignored_exceptionsfor specific exceptions you don’t want to document - Document public APIs thoroughly - Use stricter settings for public-facing code
- Consider your audience - If your API throws System exceptions that callers need to handle, document them
- Use scoped configuration - Apply different rules to different parts of your codebase
- Review called method scanning carefully -
scan_called_methods_for_exceptions = truecan be aggressive
See Also
- Visibility Levels - Configure which members are analyzed
- Quality Checks - Configure rename similarity threshold
- Advanced Configuration - Additional configuration options