467,104 Members | 1,094 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,104 developers. It's quick & easy.

clarifying

static void Main(string[] what) { string test="testing 1 2 3";
test.Replace("testing", "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(FormatException){}
thank you for clarifying


Nov 15 '05 #1
  • viewed: 1348
Share:
2 Replies
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("testing", "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.Exception e){}
cathc(System.FormatException 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" <fl***@masfd.com> wrote in message
news:3f********@news.tm.net.my...
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(FormatException){}
thank you for clarifying

Nov 15 '05 #2
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("testing", "test"); // why this doesn't
replace the word "testing" ?
}

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

public static void Main()
{
System.Text.StringBuilder sb = new StringBuilder("testing 1 2 3");
sb.Replace("testing", "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.FileMode.Open);
}
catch (ArgumentException e)
{
}
catch(FileNotFoundException e)
{
}
catch(IOException e)
{
}
finally
{
}
}

HabibH.

"smith flyers" <fl***@masfd.com> wrote in message
news:3f********@news.tm.net.my...
static void Main(string[] what) { string test="testing 1 2 3";
test.Replace("testing", "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(FormatException){}
thank you for clarifying

Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by elein | last post: by
1 post views Thread by UJ | last post: by
8 posts views Thread by ldndude | last post: by
1 post views Thread by Ben white | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.