Aug 28, 2007

The silent catch

Another of my interesting phrases (or at least I don't know if someboddy used this phrase). What is silent catch?

Silent catch is a term I use for try .. catch block, where the catch does nothing or at least you don't see it to be doing anything.

The first example:
try
{
some unsafe code here ...
}
catch {}

Another example:
try
{
some unsafe code here ...
}
catch (Exception ex)
{
hide something (for example grid or some other control) ...
return;
}

The second example is as bad as the first. It will be underlined by the Microsoft Visual Studio code editor to warn you that the ex object isn't used anywhere within the body of the catch statement.

Today I spent about two hours to investigate why a page doesn't display
one Grid. It appeared to be beried deep inside the system (I mean it was in an ascx control). The problem appeared to be missing view in the database. Unfortunatelly after I couldn't see the exception I was forced to start from the web user interface, first I checked the settings in the application to see if it is not forbidden or something, then I started to debug, to see where this part of the page comes from. After I found it, I needed to see what's going on in the control. So it took me two hours to trace the problem. What if there were an exception with stack trace? 15 minutes of work would have been enough for me to see that the view is missing.

So:
"Try to avoid silent catch statements as they may cost you a lot of efforts to find the problem later (even if you wrote the code on your own)."

2 comments:

Veselin Kolev said...

Hi, Pavka! I am very glad that you have joined our team! For the post - I am on the same opinion. I will just mention that there is a little difference between
try{}catch(Exception) and try{} catch{}. The second catches all exception including the one from unmanaged code. Keep up blogging! Best regards, Vesko Kolev

Павелъ Дончевъ said...

Thanks for the valuable post, I didn't know about this "slight" difference in both try .. catch scenarios, but you will agree with me that both of them are causing great pain for a developer, since he / she will need to start troubleshooting the problem from the user interface.