There’s no end to the number of bad programming examples we’ve seen in the past or will see in the future. Recently, I saw this one. (This was actual code seen in an actual project.)
try { createDate = Request.Params["createDate"]; }
catch (Exception) { createDate = "-1"; }
Nice and ugly. Aside from a horrible way to implement a try/catch block, it screams of performance issues and unreadable code.
A more proper alternative follows.
createDate = Request.Params["createDate"] ?? "-1";
The same project also had this use of integer parsing.
try { myInt = Int32.Parse(textBox.Text); }
catch (Exception) { myInt = -1; }
In this case, you would use the TryParse method instead:
if (Int32.TryParse(textBox.Text, out myInt))
myInt = -1;
Exception handling is for exceptions, not for null checking or validations.
Leave a reply to travis Cancel reply