Break out of Try

  • Thread starter Thread starter Guest
  • Start date Start date
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#?
 
Exit Try translates to a goto/label in C#. Goto is not recommended in
c# (or vb.net).

HTH,

Sam


How can I break out of a try clause?
In vb.net I can do 'Exit try'. How do I do that in C#?

B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.
 
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
 
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();
}
 
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();
}

That's not breaking out of the try though - that's breaking out of the
for loop.
 
So it is! I was trying to have it break out of the try and it seemed
to work, but I see that my test is incorrect! I tried a different test
and learned that break *does not* work to exit the try, in fact it
gives a compiler error.

So forget I said that!
 
I agree with James, though: there's nothing to break out of, so it
seems an odd request.

I have no idea what I would do with such a construct, so now I'm
curious: Arne.. what do you need it for?
 
your code doesn't compile in c#.

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
 
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:
your code doesn't compile in c#.

1) It was psuedo-code. It was supposed to suggest a method. It wasn't
intended to be used directly.
2) if you add the line "int A= 5, B=4;" before it (along with a class,
main etc), it does compile & run.
 
Well, yes, but what are you doing in your try block that you need to
exit it? That's what I'm curious about, if you don't mind my prying. :)
 
Oh, just because I can't think of why I would use something like that,
which means there's an opportunity for me to learn something. :)
 
Arne said:
I am curious why your are so curious.

:
It sounds really strange what you are trying to do here..
Can you post a piece of vb code where you exit try?

Andrey
 
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#.

It sounds like you should really restructure your code anyway - I can't
think of the last time I really wanted to exit a try block without
exiting either an enclosing loop, or the method etc.

If you *really* want to, you could write:

do
{
try
{
// stuff
break;
}
catch/finally etc
} while (false);

It's pretty ugly though.
 
Bruce,

Although I will never do it this way
(I typed it here so I don't know if it goes).

Try{
Do { if (a<1) break;
a--;}}
catch{}
finally{
a=10;}

:-)

Cor
 
Arne said:
I am curious why your are so curious.

:

Because he is trying to help you.

TMHO: If you come into a situation that you 'need' to exit a
try block, it sounds to me like bad design.

I never use a try-block for more then one or two statements,
and only then when i am not able to test upfront what
outcome a call will procuce.

I think you would be better off redesigning your code so you
would not have to answer questions about "why are you doing
something like...".
 
I have long running background process. The operator needs to be able to
cancel that process.
 
I have a long running background process. If the operator wants to cancel
he/she should be allowed to do so.
 
Try
While rd.Read
' Do a lot of stuff
If cancel Then
Exit Try
End If
' do some more
End While
Catch ex As Exception
Finally
rd.Close()
End Try
 
Back
Top