none
Is this an acceptable use of try/catch? RRS feed

  • Question

  • Basically I'm using the try catch to avoid an exception condition but not really interested to do anything if the exception does occur.  I have many of these, when accessing array of array type structure.  Is it efficient or is there a better way?

    public class myclass{

      public string[] pieces {get; set;}

      public myclass(string s){

      pieces = s.Split(':');

    }

    public void dosomething(int index){

      myclass MC = new myclass("test:string");

      try{

      MC.pieces[index] = "newvalue";

      }catch(){}

    }


    • Edited by sjs1978 Tuesday, October 16, 2012 9:21 PM
    Tuesday, October 16, 2012 9:20 PM

Answers

  • I would, personally, just handle this via checks instead:

    public void dosomething(int index)
    {
      myclass MC = new myclass("test:string");
    
      if (index >= 0 && index < MC.pieces.Length)
          MC.pieces[index] = "newvalue";
    }

    This is especially true if you expect this to occur in normal operation.  Exceptions are typically best used for exceptional circumstances, not normal program flow.

    By writing the code to handle this explicitly, you make it far obvious to the person maintaining your code (which is often you) that you did this on purpose, and it's not accidental.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Proposed as answer by Mike FengModerator Wednesday, October 17, 2012 9:48 AM
    • Marked as answer by sjs1978 Wednesday, October 17, 2012 1:54 PM
    Tuesday, October 16, 2012 11:07 PM
    Moderator

All replies

  • I would, personally, just handle this via checks instead:

    public void dosomething(int index)
    {
      myclass MC = new myclass("test:string");
    
      if (index >= 0 && index < MC.pieces.Length)
          MC.pieces[index] = "newvalue";
    }

    This is especially true if you expect this to occur in normal operation.  Exceptions are typically best used for exceptional circumstances, not normal program flow.

    By writing the code to handle this explicitly, you make it far obvious to the person maintaining your code (which is often you) that you did this on purpose, and it's not accidental.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Proposed as answer by Mike FengModerator Wednesday, October 17, 2012 9:48 AM
    • Marked as answer by sjs1978 Wednesday, October 17, 2012 1:54 PM
    Tuesday, October 16, 2012 11:07 PM
    Moderator
  • Thanks, i did some more reading about this and I realized I'm using this as flow control in place of if else. The reason i did this at first is because my objects are actually Lists of Lists of Lists....   so i would be checking an object that looks like

    public void dosomething(int x,int y, int z){

    myobject.level1[x].level2[y].level3[z] = "new value";

    }

    so as a lazy way without using all these if else to check the count i just surrounded the whole thing in a try catch

    because even if it failed it wasnt a big deal since i didnt wantto do anything anyway

    i will try and change it though


    • Edited by sjs1978 Tuesday, October 16, 2012 11:49 PM
    Tuesday, October 16, 2012 11:49 PM
  • Hi Sjs,

    Nice to see you.

    Does this mean you accept Reed's suggestion?

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, October 17, 2012 9:49 AM
    Moderator