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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:39:19+00:00 2026-06-06T05:39:19+00:00

I have a class containing a collection of items. For convenience I’ve provided GetCurrentItem

  • 0

I have a class containing a collection of items. For convenience I’ve provided GetCurrentItem which is implemented by

public Type GetCurrentItem
{
     get { return this.items[this.items.Count - 1]; }  
}

which will throw an exception if there are no items in the list.

Should I let the exception be thrown or should I return null? If this was an API I handed to you, what would you expect? The exception or null? Is there a better way to handle this?

  • 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-06-06T05:39:21+00:00Added an answer on June 6, 2026 at 5:39 am

    As to which is more correct? As Kirk’s comment suggests: it depends. Sometimes a null makes logical sense and sometimes an exception is better suited if no default is reasonable. One thing I try to do is think of “is calling GetCurrentItem a logical failure or a safe thing?”

    If it is a failure to call GetCurrentItem when there are none, then throwing an exception is the correct course. For example, if your collection has a HasCurrent or IsEmpty property where someone could examine the result before calling GetCurrentItem, then they should “know better”. But if the current item is null is a correct logical way of using your class, then by all means design it that way. Either way, I’d document the behavior in the code comments to let users know of expected behavior.

    I will say this though, exposing the ArgumentOutOfRange exception may be bleeding implementation details. That is, if the user of this class has no idea that the inner structure is an array or List<T>, then don’t bleed out that exception, but catch it, wrap it, and throw a more meaningful one (custom, or something like InvalidOperationException).

    Since they’re not really directly passing in an argument, them getting an ArgumentOutOfRange exception could be confusing 🙂

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

Sidebar

Related Questions

I've have an Observable collection containing customer objects: public class Customer { public string
I have a class containing a collection property which I want to display and
I have an example class containing two data points: public enum Sort { First,
I have a list of items, where each item is a simple class containing
I have a generic class containing a special collection. An instance of this collection
I have an ObservableCollection containing an ObservableCollection: public class BooksDetailModel { public BookModel Book{
I have a class containing two properties: public class player{ public player(String playerName,int points){
I have a Python class containing a list, to which I append() values. If
Let's say I have a collection of 'items' like so: <p class=item>Item 1</p> <p
I have a class containing Linq To SQL objects that are used to populate

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.