CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Eric Wise

Business & .NET

Avoiding Try - Catch

Grant Killian has a good article about why a coder shouldn't use try-catch blocks in just any situation.  I am ashamed to admit that back when I first started using .NET I was guilty of the sin he speaks of.

What I wanted to add to his post was another very common poor usage of try-catch I have seen many inexperienced coders (including myself at one point in time) do.

   try

   {

      ddlMyDropDownList.Items.FindByValue(myValue).Selected = true;

   }

   catch(Exception ex)

   {

      //Do nothing or display some kind of warning message

   }

Bad Bad Bad!  Try this instead:

   ListItem li;

   li = myDropDownList.Items.FindByValue(myValue);

   if(li != null)

       li.Selected = true;

  

Published Jan 29 2005, 08:31 AM by Eric Wise
Filed under: ,

Comments

Grant said:

amen
# January 31, 2005 10:28 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

Our Sponsors

Free Tech Publications