Break out of Try

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#?
 
S

Samuel R. Neff

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.
 
J

James Curran

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
 
C

Chris Dunaway

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();
}
 
J

Jon Skeet [C# MVP]

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.
 
C

Chris Dunaway

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!
 
B

Bruce Wood

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?
 
G

Guest

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
 
G

Guest

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

James Curran

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.
 
B

Bruce Wood

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. :)
 
B

Bruce Wood

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. :)
 
M

MuZZy

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
 
J

Jon Skeet [C# MVP]

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.
 
C

Cor Ligthert

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
 
E

Evert Timmer

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...".
 
G

Guest

I have long running background process. The operator needs to be able to
cancel that process.
 
G

Guest

I have a long running background process. If the operator wants to cancel
he/she should be allowed to do so.
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top