DCSIMG
Try{refactoring();}catch{return null;} - Doron Goldberg

Try{refactoring();}catch{return null;}

As part of fixing old legacy code (written ~5 years ago and being maintained ever since) I asked one of my programmers to search all try-catch clauses in the application and make sure log4net is logging them.

What I found was that people use try-catch a lot as part of their debugging procedure or "let's cover up our failures" and as part of the application logic. All of them are WRONG uses of the try-catch mechanism!

Some examples:

Example #1 - "LET'S PLAY CATCH!"

try
{
...
}
catch(Exception ex)
{
return null;
}

All I can say is - "If a tree falls in a forest with no one to hear it, then does it make a sound?" 

Example #2 - "LET'S PLAY CATCH AND DRIVE THE CLR MAD!"

try
{
  ...
}
catch(Exception ex)
{
  string sError = ex.Message;
}

This example demonstrates how to use try-catch for debug and keep the CLR busy! 

Example #3 - "LET'S PLAY SMART CATCH!"

try
{
objPds.PageSize = Convert.ToInt32(Page.ResultsPerPage);
}
catch
{
objPds.PageSize = 100;
}

Trying to light a match on order to make sure the match is working?

 

A more detailed list of wrong actions and the right way of doing them will be published later on as we will proceed with the project.

 

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: