exception

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Shane.H.1@gmail.com

    #1

    exception

    Hi
    Im learning C sharp, and I am trying to grok exceptions
    Using the following code name is returning "N/A" where I want it to
    return the string held by tempHold
    What am I doing wrong?

    public string Name
    {

    get{
    return name;
    }
    set{

    string tempHold = value;
    if(value.Length >14){

    //value.Length = -0;
    string tempStr = value;
    tempHold = "";
    for (int i = 0; i <=14;i++){
    tempHold += tempStr[i];
    }
    value = tempHold;
    Exception e = new Exception("Erro r the name must be less than 14
    characters long: name now equals" + tempHold);
    throw e;
    }
    name = value;
    }
    }

  • ssamuel

    #2
    Re: exception

    How about this:

    public string Name
    {
    get
    {
    return name;
    }
    set
    {
    if (value != null || value.Length < 15)
    throw new Exception(...);
    name = value;
    }
    }


    You seem to be doing many unnecessary operations and you're missing
    several little things. Simplicity breeds success, and a simpler method
    will probably give you fewer problems.


    Stephan



    Shane.H.1@gmail .com wrote:
    Hi
    Im learning C sharp, and I am trying to grok exceptions
    Using the following code name is returning "N/A" where I want it to
    return the string held by tempHold
    What am I doing wrong?
    >
    public string Name
    {
    >
    get{
    return name;
    }
    set{
    >
    string tempHold = value;
    if(value.Length >14){
    >
    //value.Length = -0;
    string tempStr = value;
    tempHold = "";
    for (int i = 0; i <=14;i++){
    tempHold += tempStr[i];
    }
    value = tempHold;
    Exception e = new Exception("Erro r the name must be less than 14
    characters long: name now equals" + tempHold);
    throw e;
    }
    name = value;
    }
    }

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: exception

      <Shane.H.1@gmai l.comwrote:
      Im learning C sharp, and I am trying to grok exceptions
      Using the following code name is returning "N/A" where I want it to
      return the string held by tempHold
      What am I doing wrong?
      Well, you're only setting name if the value that's passed in is valid -
      and that seems only reasonabe. An exception is being thrown otherwise.
      The exception is thrown instead of the property being set, and that's
      usually a useful semantic: it would be quite odd for name to be changed
      *and* an exception to be thrown. (Where possible, it's useful for a
      method to make no visible changes if it throws an exception.)

      You should also look up String.Substrin g, by the way: your current code
      is equivalent to (but less efficient and harder to read than):

      public string Name
      {
      get
      {
      return name;
      }
      set
      {
      if (value.Length 14)
      {
      throw new Exception
      ("Error the name must be less than " +
      "14 characters long: name now equals" +
      value.Substring (0, 14));
      }
      name = value;
      }
      }

      Now, if you *do* want the semantics suggested by the text of the
      exception, use:

      public string Name
      {
      get
      {
      return name;
      }
      set
      {
      if (value.Length 14)
      {
      name = value.Substring (0, 14);
      throw new Exception
      ("Error the name must be less than " +
      "14 characters long: name now equals" +
      name);
      }
      else
      {
      name = value;
      }
      }
      }

      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      If replying to the group, please do not mail me too

      Comment

      • Shane.H.1@gmail.com

        #4
        Re: exception


        Shane.H.1@gmail .com wrote:
        Hi
        Im learning C sharp, and I am trying to grok exceptions
        Using the following code name is returning "N/A" where I want it to
        return the string held by tempHold
        What am I doing wrong?

        public string Name
        {

        get{
        return name;
        }
        set{

        string tempHold = value;
        if(value.Length >14){

        //value.Length = -0;
        string tempStr = value;
        tempHold = "";
        for (int i = 0; i <=14;i++){
        tempHold += tempStr[i];
        }
        value = tempHold;
        Exception e = new Exception("Erro r the name must be less than 14
        characters long: name now equals" + tempHold);
        throw e;
        }
        name = value;
        }
        }
        ssamuel wrote:
        How about this:
        >
        public string Name
        {
        get
        {
        return name;
        }
        set
        {
        if (value != null || value.Length < 15)
        throw new Exception(...);
        name = value;
        }
        }
        >
        >
        You seem to be doing many unnecessary operations and you're missing
        several little things. Simplicity breeds success, and a simpler method
        will probably give you fewer problems.
        >
        >
        Stephan
        >
        >
        >
        Hi,
        Yeah the only thing is I want name updated if its too long
        Using your suggestion I could ask the user to re-enter new details but
        I want to handle the issue in the code
        Also, with your suggestion name is given value, which may be too long
        I only want name (in this case) to be 0 - 14 chars long

        Comment

        • sloan

          #5
          Re: exception

          Huh??
          Why are you looping on it?

          public string Name
          {
          set
          {

          bool badName = false;

          if( null==value)
          {
          badName = true;
          }
          else
          {
          if(value.Length >14)
          {
          badName = true;
          }
          }

          if (badName==true)
          {
          throw new ArgumentExcepti on("Name must be < 14 characters");
          }

          m_name = value;

          }
          }


          <Shane.H.1@gmai l.comwrote in message
          news:1163445640 .212418.155740@ f16g2000cwb.goo glegroups.com.. .
          Hi
          Im learning C sharp, and I am trying to grok exceptions
          Using the following code name is returning "N/A" where I want it to
          return the string held by tempHold
          What am I doing wrong?
          >
          public string Name
          {
          >
          get{
          return name;
          }
          set{
          >
          string tempHold = value;
          if(value.Length >14){
          >
          //value.Length = -0;
          string tempStr = value;
          tempHold = "";
          for (int i = 0; i <=14;i++){
          tempHold += tempStr[i];
          }
          value = tempHold;
          Exception e = new Exception("Erro r the name must be less than 14
          characters long: name now equals" + tempHold);
          throw e;
          }
          name = value;
          }
          }
          >

          Comment

          • Shane.H.1@gmail.com

            #6
            Re: exception


            Jon wrote:
            <Shane.H.1@gmai l.comwrote:
            Im learning C sharp, and I am trying to grok exceptions
            Using the following code name is returning "N/A" where I want it to
            return the string held by tempHold
            What am I doing wrong?
            >
            Well, you're only setting name if the value that's passed in is valid -
            and that seems only reasonabe. An exception is being thrown otherwise.
            The exception is thrown instead of the property being set, and that's
            usually a useful semantic: it would be quite odd for name to be changed
            *and* an exception to be thrown. (Where possible, it's useful for a
            method to make no visible changes if it throws an exception.)
            >
            You should also look up String.Substrin g, by the way: your current code
            is equivalent to (but less efficient and harder to read than):
            >
            public string Name
            {
            get
            {
            return name;
            }
            set
            {
            if (value.Length 14)
            {
            throw new Exception
            ("Error the name must be less than " +
            "14 characters long: name now equals" +
            value.Substring (0, 14));
            }
            name = value;
            }
            }
            >
            Now, if you *do* want the semantics suggested by the text of the
            exception, use:
            >
            public string Name
            {
            get
            {
            return name;
            }
            set
            {
            if (value.Length 14)
            {
            name = value.Substring (0, 14);
            throw new Exception
            ("Error the name must be less than " +
            "14 characters long: name now equals" +
            name);
            }
            else
            {
            name = value;
            }
            }
            }
            >
            --
            Jon Skeet - <skeet@pobox.co m>
            http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
            If replying to the group, please do not mail me too
            Sweet, thats exactly what needed to be done, as is probably obvious Im
            not overly familiar with SubString, but thats how I learn right :-)

            Comment

            • ssamuel

              #7
              Re: exception

              Yeah the only thing is I want name updated if its too long

              Huh? You want it updated *then* you want to throw the exception?

              That's easy:

              set
              {
              name = value;
              if (name == null || name.Length 14)
              throw new Exception(...);
              }

              There's almost definitely a better way to implement that logic,
              however.

              Using your suggestion I could ask the user to re-enter new details but
              I want to handle the issue in the code
              In a property is never the correct place to ask the user for more or
              different information. Your best bet here is to do something more
              robust with user input. Try something like getting the input, then
              immediately checking if it's valid.

              Also, with your suggestion name is given value, which may be too long
              I only want name (in this case) to be 0 - 14 chars long
              I'm sure you can figure out how to tweak the exact logic (and <
              signs, etc.) to fit what you're trying to do.

              Are you sure you understand the symantic meaning of classes and
              properties? It seems like you're trying to write procedural (non-OOP)
              code and shoehorn it into a class.


              Stephan

              Comment

              Working...