473,405 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Break out of Try

How can I break out of a try clause?
In vb.net I can do 'Exit try'. How do I do that in C#?
Nov 17 '05 #1
18 45173

Exit Try translates to a goto/label in C#. Goto is not recommended in
c# (or vb.net).

HTH,

Sam
On Mon, 4 Apr 2005 09:40:46 -0700, "Arne"
<Ar**@discussions.microsoft.com> wrote:
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.
Nov 17 '05 #2
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" <Ar**@discussions.microsoft.com> wrote in message
news:A5**********************************@microsof t.com...
How can I break out of a try clause?
In vb.net I can do 'Exit try'. How do I do that in C#?

Nov 17 '05 #3
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();
}

Nov 17 '05 #4
Chris Dunaway <du******@gmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5
your code doesn't compile in c#.

"James Curran" wrote:
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" <Ar**@discussions.microsoft.com> wrote in message
news:A5**********************************@microsof t.com...
How can I break out of a try clause?
In vb.net I can do 'Exit try'. How do I do that in C#?


Nov 17 '05 #6
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#.

"Bruce Wood" wrote:
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?

Nov 17 '05 #7
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?

Nov 17 '05 #8
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!

Nov 17 '05 #9
"Arne" <Ar**@discussions.microsoft.com> wrote in message
news:36**********************************@microsof t.com...
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.
Nov 17 '05 #10
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. :)

Nov 17 '05 #11
I am curious why your are so curious.

"Bruce Wood" wrote:
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. :)

Nov 17 '05 #12
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. :)

Nov 17 '05 #13
Arne wrote:
I am curious why your are so curious.

"Bruce Wood" wrote:

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

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

Andrey
Nov 17 '05 #14
Arne <Ar**@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #15
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


Nov 17 '05 #16
Arne wrote:
I am curious why your are so curious.

"Bruce Wood" wrote:

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


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...".
Nov 17 '05 #17
Jon,
A good reason?
I have to claim the fifth amendment on that one.

"Jon Skeet [C# MVP]" wrote:
Arne <Ar**@discussions.microsoft.com> wrote:
Yes, sometimes I throw exceptions to get out. It will not work at this time.


So either return, or set a flag to say what you need to do when you
break out of the existing loop. You still haven't given a good reason
for adding an extra loop.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #18
Arne <Ar**@discussions.microsoft.com> wrote:
A good reason?
I have to claim the fifth amendment on that one.


That's fine - just don't say we didn't warn you...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ann | last post by:
I have trouble sometimes figuring out where break and continue go to. Is there some easy way to figure it out, or a tool? TIA Ann
5
by: viza | last post by:
Hi! Suppose I have int i,j,k; for(i=0;i<I;++i){ /* loop 1 */ for(j=0;j<J;++j){ /* loop 2 */ for(k=0;k<K;++k){ /* loop 3 */ if(test){
25
by: chunhui_true | last post by:
In <<expert c>>I know the break in if wich is scoped in switch is break the switch,like: switch c case 1: if(b){ break; } ...... But like this: while(a){
5
by: tony collier | last post by:
To break out of a loop i have seen some people use RETURN instead of BREAK I have only seen RETURN used in functions. Does anyone know why RETURN is used instead of BREAK to kill loops?
55
by: Ennixo | last post by:
hi, do you know where i can find some ebooks or websites talking about C# optimisation ? for exemple, i just learned that ++i is faster than i++. i would like to know more about the things...
2
by: yyhhjj | last post by:
I created a test program to implement an iterator. First, I used 'yield break' in the iterator, it worked normally. Then, I simply used 'break' in the places of 'yield break', it still worked...
3
by: Barbara Alderton | last post by:
I recently wrote an ASP.NET app that used user controls. I produced a printer-friendly output form. One requirement was a pagebreak between one part of the form and another. I had no problem...
2
by: ichor | last post by:
hi i want to add a conditional break in my vb.net code. like we could in vb6. for example if the counter = 10 then break at that point. kindly let me know if thats possible in vb.net Ichor
26
by: Alexander Korsunsky | last post by:
Hi! I have some code that looks similar to this: -------------------------------------------- char array = "abcdefghij"; for (int i = 0; i < 10; i++) {
7
by: =?Utf-8?B?aXdlYg==?= | last post by:
Can we set page breaks in ASP Pages dynamically based on the data being populated? Akshay.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.