Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Commenting try catch statements

What is the proper place to explain error handling in a try-catch statement? It seems like you could put explanatory comments at either the beginning of the try block or the catch block.

// Possible comment location 1
try
{   
    // real code
}
// Possible comment location 2
catch
{
    // Possible comment location 3

    // Error handling code

}
like image 688
Eric Ness Avatar asked Nov 26 '22 23:11

Eric Ness


1 Answers

I usually do the following. If there's only one exception being handled, I usually don't bother since it should be self-documenting.

try
{   
    real code // throws SomeException
    real code // throws SomeOtherException
}
catch(SomeException se)
{
    // explain your error handling choice if it's not obvious
}
catch(SomeOtherException soe)
{
    // explain your error handling choice if it's not obvious
}
like image 167
Bill the Lizard Avatar answered Dec 05 '22 14:12

Bill the Lizard