GithubHelp home page GithubHelp logo

nangal / exceptionanalyzer Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sergeyteplyakov/exceptionanalyzer

0.0 3.0 0.0 831 KB

Roslyn-based analyzer for exception handling best practices

License: MIT License

Batchfile 0.71% C# 98.87% PowerShell 0.41%

exceptionanalyzer's Introduction

Roslyn-based Exception Analyzer

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.

New exception should have original exception as InnerException (v.1.0.5)

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:

Image1

Empty catch block considered harmful!

Application code should not swallow all exceptions using generic catch blocks like catch {} or catch(Exception){}. Image1

Swallow exceptions considered harmful

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.

Image2

Catch block swallows an exception

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.

Image3

Rethrow exception properly

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.

Image4

Tracing ex.Message considered harmful

Exception object is very complex and could form tree-like structure with InnerExceptions. 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.

Image5

exceptionanalyzer's People

Contributors

antoinega avatar sergeyteplyakov avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.