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 4235708
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:32:15+00:00 2026-05-21T02:32:15+00:00

I have the following scenario: I have a foreach loop that loops through a

  • 0

I have the following scenario:

  1. I have a foreach loop that loops through a collection, if an item is found (based on criteria as in example below), it will return that item.

If not what is the most graceful way to deal with this exception. I have a throw new notimplementedexception but I think there is a more elegant way.

Code is:

 foreach (SPContentType type in sPContentTypeCollection)
            {
                if (type.Name == contentTypeName)
                {
                    return type; 
                }
            }

 throw new NotImplementedException(); 

As you can see, it’s not what I would call readable code. How can I make it easier for the next guy to maintain. On a side note, it does what it should from a technical perspective.

  • 1 1 Answer
  • 0 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. Editorial Team
    Editorial Team
    2026-05-21T02:32:16+00:00Added an answer on May 21, 2026 at 2:32 am

    Well NotImplementedException is certainly inappropriate, because you have implemented the method… but what sort of exception should you throw? It really depends on the context, to be honest.

    Sometimes it would be appropriate to return null to indicate a missing value. Other times, throwing an exception is fine – possibly an InvalidOperationException for example. You should throw an exception if this situation represents an error of some description, rather than it being a perfectly reasonable situation which the caller should expect to occur.

    As for the rest of the code… if you’re using .NET 3.5 you could just use LINQ:

    return sPContentTypeCollection.Cast<SPContentType>()
                                  .First(type => type.Name == contentTypeName);
    

    That will throw an InvalidOperationException for you automatically if the name isn’t found. Or if you want to return null:

    // type shortened to t to avoid scrollbars...
    return sPContentTypeCollection.Cast<SPContentType>()
                                  .FirstOrDefault(t => t.Name == contentTypeName);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine the following scenario. I have a checkbox that the user can check to
I have the following code @foreach (var item in Model.Defaults) { <tr class=CertainCategory> <td>
I have the following scenario. I have one table in the database A that
I have the following scenario: My page has a dropdown list that allows user
I have the following scenario: there are a database that generates a new logTable
I have the following scenario I have a main form as MDI parent and
I have a following scenario – and what I am really looking is real
I have the following scenario: Entities are loaded from the database. One of them
I have the following scenario where I want to pass in string and a
I have the following scenario. I have created an ASP.NET web application (framework 3.5)

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.