Proper exception handling is a common issue in many projects. There is several common code smells but most of them would be obvious for everyone with this analyzer.
This analyzer catches following issues.
Catch block could throw another exception that would be more meaningful for the clients. But in this case original exception should not be forgotten.
Tool will warn about this and provide a fix that will add original excpetion to InnerException
if possible:
Application code should not swallow all exceptions using generic catch blocks like catch {}
or catch(Exception){}
.
Empty catch blocks are dangerous, but even when the catch
block is not empty it still could swallow exceptions.
catch
blocks without throw;
statement are harmful, because they swallow all exceptions (even non-CLS-compliant) without any chances for proper handling (even tracing is impossible).
This analyzer will warn about such kind of issue and will provide a fix that will add throw
statement at the bottom of catch
block.
There is another warning related to swallowed exceptions.
Even non-empty catch block could rethrow exceptions in some cases and swallow them in another. This analyzer will warn for every code path in the catch block that swallows generic exceptions.
There is no fix for such kind of issues yet (replace return
to throw
?) but even in this case you'll see that you're hiding exceptions in some cases.
Incorrect exception propagation is very common. Difference between throw ex
and throw
in the catch(Exception ex)
block is subtle but yet important.
First statement will rethrow original exception but will override the stack trace. throw
statement on the other hand will propagate original excpetion as is.
Exception object is very complex and could form tree-like structure with InnerException
s. Unfortunately, there is a planty of code in the wild that uses absolutely insane way for tracing exception and store only Message
property.
Everytime developer stores only ex.Message
in log-file God kills a kitty! You should save cats and save important information about exceptions!
This analyzer will warn if you just store Message
and observe exceptions inappropriately.