Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 32853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T13:49:43+00:00 2026-05-10T13:49:43+00:00

Imagine an object you are working with has a collection of other objects associated

  • 0

Imagine an object you are working with has a collection of other objects associated with it, for example, the Controls collection on a WinForm. You want to check for a certain object in the collection, but the collection doesn’t have a Contains() method. There are several ways of dealing with this.

  • Implement your own Contains() method by looping through all items in the collection to see if one of them is what you are looking for. This seems to be the ‘best practice’ approach.
  • I recently came across some code where instead of a loop, there was an attempt to access the object inside a try statement, as follows:
try   {       Object aObject = myCollection[myObject];   }   catch(Exception e)   {       //if this is thrown, then the object doesn't exist in the collection } 

My question is how poor of a programming practice do you consider the second option be and why? How is the performance of it compared to a loop through the collection?

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T13:49:44+00:00Added an answer on May 10, 2026 at 1:49 pm

    I would have to say that this is pretty bad practice. Whilst some people might be happy to say that looping through the collection is less efficient to throwing an exception, there is an overhead to throwing an exception. I would also question why you are using a collection to access an item by key when you would be better suited to using a dictionary or hashtable.

    My main problem with this code however, is that regardless of the type of exception thrown, you are always going to be left with the same result.

    For example, an exception could be thrown because the object doesn’t exist in the collection, or because the collection itself is null or because you can’t cast myCollect[myObject] to aObject.

    All of these exceptions will get handled in the same way, which may not be your intention.

    These are a couple of nice articles on when and where it is usally considered acceptable to throw exceptions:

    • Foundations of Programming
    • Throwing exceptions in c#

    I particularly like this quote from the second article:

    It is important that exceptions are thrown only when an unexpected or invalid activity occurs that prevents a method from completing its normal function. Exception handling introduces a small overhead and lowers performance so should not be used for normal program flow instead of conditional processing. It can also be difficult to maintain code that misuses exception handling in this way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example, say the main Activity has an custom object foo with a method
I have a question. Imagine you'd have an object you'd want to save in
Imagine you have an object foo that you saved as saved.file.rda as follows: foo
Question from Object-Oriented JavaScript book: Imagine Array() doesn't exist and the array literal notation
Imagine an interface with a method to create objects of type Address . The
Imagine I want to draw a pyramid made of triangles. -Should I create a
I have a WPF application which so far has been client only, but now
So, I'm working on designing a class wherein if certain arguments to certain methods
I have a BitmapFrame object created by a working thread (not UI thread) and
I am working on a 2d game. Imagine a XY plane and you are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.