Eric Wise

Sponsors

The Lounge

Wicked Cool Jobs

Blogs I Read

Fun & Games

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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;

  

Posted Sat, Jan 29 2005 8:31 AM by Eric Wise
Filed under: ,

[Advertisement]

Comments

Grant wrote re: Avoiding Try - Catch
on Mon, Jan 31 2005 10:28 AM
amen
Devlicio.us