The ExceptionatorRoslyn analyzers for improving exception handling in C# code. This project includes a set of diagnostics aimed at encouraging better exception practices. Each rule detects common pitfalls and helps improve reliability, maintainability, and clarity of your exception logic. RulesEX001: Exception should include a messageEnsures that exceptions are instantiated with a meaningful message. ❌ Bad:
✅ Good:
EX002: Avoid throwing base exceptionsAvoids throwing base exceptions like System.Exception or System.SystemException. ❌ Bad:
✅ Good:
EX003: Missing inner exceptionEnsures that newly thrown exceptions inside catch blocks include the original exception as inner exception. ❌ Bad:
✅ Good:
EX004: Use 'throw;' instead of 'throw ex;'Preserves the original stack trace when rethrowing exceptions. ❌ Bad:
✅ Good:
EX005: Exception variable is unusedDetects catch variables that are never used. ❌ Bad:
✅ Good:
EX006: Unreachable code after throwDetects code written after a throw statement that will never execute. ❌ Bad:
✅ Good:
EX007: Pointless try/catch blockDetects try/catch blocks that don’t add meaningful handling logic. ❌ Bad:
✅ Good:
EX008: ThreadAbortException must not be swallowedEnsures ThreadAbortException is either rethrown or reset. ❌ Bad:
✅ Good:
EX009: Empty try block with catchDetects try blocks that are empty while having a catch. ❌ Bad:
✅ Good:
EX010: Task.WaitAll should be wrapped with AggregateException catchEnsures proper exception handling for Task.WaitAll by requiring AggregateException catch or using Task.WhenAll. ❌ Bad:
✅ Good:
EX011: Empty catch blockDetects catch blocks that are completely empty without even a comment. ❌ Bad:
✅ Good:
EX012: Don't throw exceptions from property gettersDiscourages throwing exceptions directly from property getters. ❌ Bad:
✅ Good:
EX013: Avoid throwing ex.InnerExceptionDetects when ex.InnerException is thrown directly, which may cause null reference issues and loses the stack trace. ❌ Bad:
✅ Good:
EX014: Avoid logging only ex.MessageSuggests logging the full exception instead of just the message to retain full context. ❌ Bad:
✅ Good:
EX015: Avoid logging ex.ToString()Recommends logging the exception directly rather than calling ToString. ❌ Bad:
✅ Good:
EX016: Avoid empty catch when throwing new exception without messageDetects cases where a new exception is thrown in a catch block without message or inner exception. ❌ Bad:
✅ Good:
EX017: Avoid when clauses that always evaluate to trueDetects ❌ Bad:
✅ Good:
EX018: NotImplementedException left in codeDetects ❌ Bad:
✅ Good:
EX019: Avoid general catch-all without any handlingDetects general catch blocks that don’t include logging, rethrow, or even a comment. ❌ Bad:
✅ Good:
EX020: Exception class should be publicEnsures that exception types are declared ❌ Bad:
✅ Good:
EX021: Missing expected constructors on custom exceptionEnsures that custom exceptions implement the expected constructors with message and inner exception parameters. ❌ Bad:
✅ Good:
EX022: Exception constructors must call baseEnsures that exception constructors pass their parameters (message, innerException) to the base constructor. ❌ Bad:
✅ Good:
EX023: Exception class name must end with 'Exception'Ensures consistency and clarity by requiring exception classes to follow the naming convention of ending with 'Exception'. ❌ Bad:
✅ Good:
EX024: Avoid catching fatal exceptions like StackOverflowException or ExecutionEngineExceptionFlags catch blocks that handle fatal exceptions which should not be caught or are uncatchable. ❌ Bad:
✅ Good:
AcknowledgmentsSponsored by elmah.io. |