clarifying

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • smith flyers

    clarifying

    static void Main(string[] what) { string test="testing 1 2 3";
    test.Replace("t esting", "test"); } // why this doesn't replace the word
    "testing" ?

    when using "try" can i use multiple "block catch" ? // i tried but can't
    , this is allowed in java right, how about C#?
    ex. catch(exception e) {} catch(FormatExc eption){}


    thank you for clarifying




  • William Ryan

    #2
    Re: clarifying

    Change your proc like this so that you assign test = test.Replace


    static void Main(string[] what) {
    string test="testing 1 2 3";
    test = test.Replace("t esting", "test"); //change this line.
    } // why this doesn't replace the word "testing" ?

    You can and often should have multiple catch blocks. Remember though that
    you need to go from most precise to most broad.

    If you tried this

    catch(System.Ex ception e){}
    cathc(System.Fo rmatException f){}

    The compiler will yell about unreachable code. Why? B/c the First catch
    block will grab any exception, so if a FormatException was thrown, it would
    get caught in the first catch, not the second. If you flip them around, all
    will be well. This is important b/c in VB.NET, it wont' bark at you and
    could inject subtle logic errors ---and even outside of VB.NET, remember
    that the Exceptions are processed in order - they don't skip to the
    corresponding handler .

    HTH,

    Bill

    "smith flyers" <flyer@masfd.co m> wrote in message
    news:3fad442f_2 @news.tm.net.my ...[color=blue][color=green]
    > >[/color]
    > when using "try" can i use multiple "block catch" ? // i tried but[/color]
    can't[color=blue]
    > , this is allowed in java right, how about C#?
    > ex. catch(exception e) {} catch(FormatExc eption){}
    >
    >
    > thank you for clarifying
    >
    >
    >
    >[/color]


    Comment

    • Habib Heydarian [MSFT]

      #3
      Re: clarifying

      In .NET, strings are immutable meaning that you cannot change their value.
      All the operations on a string result in a new string. In this case,
      String.Replace( ) returns a new string with the word "testing" replaced. You
      can see it from this example where newValue contains the new string.

      public static void Main()
      {
      string test="testing 1 2 3";
      string newValule = test.Replace("t esting", "test"); // why this doesn't
      replace the word "testing" ?
      }

      Try using System.Text.Str ingBuilder instead, which does exactly what you
      want:

      public static void Main()
      {
      System.Text.Str ingBuilder sb = new StringBuilder(" testing 1 2 3");
      sb.Replace("tes ting", "test");
      string newValue = sb.ToString();
      }

      Yes, you can have multiple catch blocks when using try. Here's an example:

      public static void Main()
      {
      try
      {
      FileStream fs = File.Open(@"C:\ Foo.txt", System.IO.FileM ode.Open);
      }
      catch (ArgumentExcept ion e)
      {
      }
      catch(FileNotFo undException e)
      {
      }
      catch(IOExcepti on e)
      {
      }
      finally
      {
      }
      }

      HabibH.

      "smith flyers" <flyer@masfd.co m> wrote in message
      news:3fad442f_2 @news.tm.net.my ...[color=blue]
      > static void Main(string[] what) { string test="testing 1 2 3";
      > test.Replace("t esting", "test"); } // why this doesn't replace the[/color]
      word[color=blue]
      > "testing" ?
      >
      > when using "try" can i use multiple "block catch" ? // i tried but[/color]
      can't[color=blue]
      > , this is allowed in java right, how about C#?
      > ex. catch(exception e) {} catch(FormatExc eption){}
      >
      >
      > thank you for clarifying
      >
      >
      >
      >[/color]


      Comment

      Working...