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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:04:16+00:00 2026-05-15T17:04:16+00:00

In the example code below I am taking a list of strings and concatenating

  • 0

In the example code below I am taking a list of strings and concatenating them to a delimited string. The problem occurs when I use the setter with an empty list. The ToString method throws an ArgumentOutOfRangeException since the second parameter is a -1.

How should conditions (exceptions thrown in getters/setters) like this be handled?

I don’t like the fact that the setter throws an exception since the caller doesn’t know about the internals of the class and therefor should not have to handle (or even know how) the exception. Catching all exceptions in the getter/setter and quietly handling them also sounds like a bad idea since the caller will not know the getter/setter failed.

//I realize that this isn't the best code but I wanted to produce an example
//to demonstrate my question.
private string theStringVariable;
const string DELIMITER = ",";

public IList<string> StringList
{
   set
   {
      StringBuilder stringBuilder = new StringBuilder();
      foreach(string entry in value)
      {
         stringBuilder.Append(entry);
         stringBuilder.Append(DELIMITER);
      }
      theStringVariable = stringBuilder.ToString(0, stringBuilder.Length - 1);
   }
}
  • 1 1 Answer
  • 1 View
  • 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-15T17:04:17+00:00Added an answer on May 15, 2026 at 5:04 pm

    You should check for the potential, common error conditions, and throw your own exception (prior to the StringBuilder errors) right up front, with a meaningful error message.

    In your case, you’ll most likely want to use some form of ArgumentException if the setter is called with an Empty string list. The key here is that your exception can say “the argument contained an empty collection” instead of “index out of bounds”, which is going to make the caller understand, immediately, why they have a “real” problem in their call.


    On a side note: In cases like the code you posted – I’d also consider making this a method instead of a property. You’re doing quite a bit of “work” in this property setter, which is going to be somewhat unexpected. By making this a method, you’ll be giving the user a clue that there’s a bit of “processing” occurring within this property…

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

Sidebar

Related Questions

For this code example below, usually I would use [self fall]; instead of the
The example code below works as as a server process. But when I add
I know how to do this... I'll give example code below. But I can't
UPDATE 2011.09.13 This bug has been resolved by Adobe. The example code below now
Given the example source code below, is it possible for someone to see the
For example, I have the java code below: URL u = new URL(http://google.com); URLConnection
I have a basic PHP question, take the code below for example, let's say
The example below shows the code I am using to test whether a user
Below is an example class hierarchy and code. What I'm looking for is a
For the example below: if a == 100: # Five lines of code elif

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.