How to comment why a catch can be ignored

Viewed 245

I am getting a message on Sonar:

Handle the exception or explain in a comment why it can be ignored.

How do I get rid off this warning by providing a comment explaining to ignore this?

I am using C# for coding and my catch block is catch {}

catch
{
 // handled globally
}
finally
{
 connection.Close()
}

Catch code and on finally block I have added some code to close connection.

2 Answers

That exception isn’t handled globally, it’s thrown away. You should use a pattern of:

try{
  //code
}
finally {
  //close connection
}

This removes the warning from Sonar and makes your apparent intentions true (that the global handler will deal with the exception) otherwise you’re just going to silently lose every error this code throws which will cause your help desk team no end of joy

I’ve declined to answer the question as stated for this reason, but the answer you seek is in Sonar’s fine manual if you desperately want to go that route; it will bite you

Below is your original code. The comment inside the catch block is bit confusing. The catch block in the code snippet is either a global exception handler or it is not. So let's understand both the cases in detail.

catch
{
    //handled globally
}
finally
{
    connection.Close()
}

catch block in the code above is a catch-all exception block i.e. generic exception handler for all kinds of exceptions. Above is an alternate/shortcut way of writing:

catch(Exception ex)
{
    //Generic exception handler: Eat all excetion here.
}
finally
{
    connection.Close()
}

You're handling an exception but not dealing with it in any way e.g. logging the exception details in a file. That's why Sonar is complaining with the error that you've mentioned. To overcome the warning you can do either of two approaches based on your condition:

  1. Do not handle exception (Not a global exception handler): If you do not want to handle the exception then remove the catch block or rethrow the exception explicitly. This happens when there is already a global exception handler somewhere else which will handle the exception when it bubbles up the call stack :

    try{
          //code
    }
    finally {
        //close connection
    }
    

    OR

    catch(Exception ex)
    {
        //bubble up the exception for global exception handler
        throw ex;
    }
    finally
    {
        connection.Close()
    }
    

    OR

    catch(Exception)
    {
        //bubble up the exception for global exception handler
        throw;
    }
    finally
    {
        connection.Close()
    }
    
  2. Logging (Global exception handler): Add a log statement inside the catch block as shown in the code-snippet below:

    catch(Exception ex)
    {
        Console.WriteLine(ex.ToString());
        //throw ex; //optionally you can rethrow the exception even after logging it if you catch block is not a global exception handler
    }
    finally
    {
        connection.Close()
    }
    

    The log statement inside catch block gives an indication to Sonar that we're dealing with the exception and hence suppresses the warning.

Related