G
Guest
How can I break out of a try clause?
In vb.net I can do 'Exit try'. How do I do that in C#?
In vb.net I can do 'Exit try'. How do I do that in C#?
How can I break out of a try clause?
In vb.net I can do 'Exit try'. How do I do that in C#?
Chris Dunaway said:Have you tried the break statement?
public static void Main()
{
try{
for(int i=0;i<10;i++){
Console.WriteLine(i.ToString());
if (i == 5) break;
}
}
catch {};
Console.WriteLine("After the Break Statement");
Console.ReadLine();
}
James Curran said:A "try" is not a loop. There is nothing to "break" out of. If you want
to change the flow of your code, just ignore the "try" keyword (and the
entire catch block). For example, if you think you want:
try
{
/// do stuff
if (A == B)
exit try;
// do more stuff when A!=B
}
catch
{
// log error
}
// continue here
image it as
/// do stuff
if (A == B)
exit try;
// do more stuff when A!=B
// continue here
which should make the answer obvious:
try
{
/// do stuff
if (A != B)
{
// do more stuff when A!=B
}
}
catch
{
// log error
}
// continue here
Arne said:your code doesn't compile in c#.
It sounds really strange what you are trying to do here..Arne said:I am curious why your are so curious.
:
Arne said:I need to exit my try block the same way I do in vb.Net with 'exit try'. This
only works in VB.Net and it is not available in c#.
Arne said:I am curious why your are so curious.
: