|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Break out of TryHow 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 On Mon, 4 Apr 2005 09:40:46 -0700, "Arne" <A***@discussions.microsoft.com> wrote: >How can I break out of a try clause? B-Line is now hiring one Washington D.C. area VB.NET >In vb.net I can do 'Exit try'. How do I do that in C#? 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 Show quoteHide quote "Arne" <A***@discussions.microsoft.com> wrote in message news:A59EEABA-3F46-4E9F-88F1-223584CBD765@microsoft.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#? your code doesn't compile in c#.
Show quoteHide quote "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" <A***@discussions.microsoft.com> wrote in message > news:A59EEABA-3F46-4E9F-88F1-223584CBD765@microsoft.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#? > > > "Arne" <A***@discussions.microsoft.com> wrote in message 1) It was psuedo-code. It was supposed to suggest a method. It wasn'tnews:36ABAA5C-111C-4186-891F-CDC7A720594D@microsoft.com... > your code doesn't compile in c#. 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. 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 <dunaw***@gmail.com> wrote:
Show quoteHide quote > Have you tried the break statement? That's not breaking out of the try though - that's breaking out of the > > 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(); > } for loop. -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too 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? 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#. Show quoteHide quote "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? > > 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. :) I am curious why your are so curious.
Show quoteHide quote "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. :) > > 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. :) I have long running background process. The operator needs to be able to
cancel that process. Show quoteHide quote "Bruce Wood" wrote: > 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 wrote:
> I am curious why your are so curious. It sounds really strange what you are trying to do here..> > "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. :) >> >> Can you post a piece of vb code where you exit try? Andrey 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 Show quoteHide quote "MuZZy" wrote: > 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 >
Show quote
Hide quote
"Arne" <A***@discussions.microsoft.com> wrote in message No Problem, just do what Chris suggestednews:A7F5281D-A105-4A21-820E-BACDDB265644@microsoft.com... > 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 > try { while (rd.Read) { DoStuff(); if (cancel) break; DoMoreStuff(); } } catch (Exception ex) { DoCatchStuff(); } Finally { rd.Close(); } "break;" gets you out of the while loop which in your case, causes it to leave the try block The finally will still get executed Hope this helps Bill Bill,
That will not work because after my while loop I have other stuff that I optionally do not want to executed. Show quoteHide quote "Bill Butler" wrote: > "Arne" <A***@discussions.microsoft.com> wrote in message > news:A7F5281D-A105-4A21-820E-BACDDB265644@microsoft.com... > > 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 > > > > No Problem, just do what Chris suggested > > try > { > while (rd.Read) > { > DoStuff(); > if (cancel) > break; > DoMoreStuff(); > } > } > catch (Exception ex) > { > DoCatchStuff(); > } > Finally > { > rd.Close(); > } > > "break;" gets you out of the while loop > which in your case, causes it to leave the try block > > The finally will still get executed > Hope this helps > Bill > > > "Arne" <A***@discussions.microsoft.com> wrote in message Hi Arne,news:8A4682FA-11B4-4590-955A-D5D020B947A5@microsoft.com... > Bill, > That will not work because after my while loop I have other stuff that I > optionally do not want to executed. > Bruce has already given you a modified version that allows this case as well. Here is an alternative that puts all of the work inside of a method. Depending upon your exact situation, one may be more preferable that another. I can't imagine a situation where one of these techniques would not work. The downside is that you may have to deal with scoping issues. For instance local variables will be out of scope in the called method. The upside is that you can return out of deeply nested loops. try { ReadFunc(rd); } catch (Exception ex) { DoCatchStuff(); } Finally { rd.Close(); } public void ReadFunc(RDTYPE rd) { while (rd.Read) { DoStuff(); if (cancel) return; DoMoreStuff(); } DoYetMore(); return; } It is often considered bad form to have multiple "return" statements from a method. Sometimes it is the best way to go. Hope this helps Bill Arne wrote:
> I am curious why your are so curious. Because he is trying to help you.> > "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. :) >> >> 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 a long running background process. If the operator wants to cancel
he/she should be allowed to do so. Show quoteHide quote "Evert Timmer" wrote: > 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...". > "Arne" <A***@discussions.microsoft.com> wrote in message Then what you actually have is a loop, which you want to break out of.news:7F796C03-1263-4F44-A2AB-90F7B5871D11@microsoft.com... > I have a long running background process. If the operator wants to cancel > he/she should be allowed to do so. The fact that it's in a Try is irrelevant. I can execute a break in the while loop, but I can't break out the Try. I
have other things after the while loop which I don't want to execute. Show quoteHide quote "James Curran" wrote: > "Arne" <A***@discussions.microsoft.com> wrote in message > news:7F796C03-1263-4F44-A2AB-90F7B5871D11@microsoft.com... > > I have a long running background process. If the operator wants to cancel > > he/she should be allowed to do so. > > Then what you actually have is a loop, which you want to break out of. > The fact that it's in a Try is irrelevant. > > > 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;} :-) CorArne <A***@discussions.microsoft.com> wrote:
> I need to exit my try block the same way I do in vb.Net with 'exit try'. This It sounds like you should really restructure your code anyway - I can't > only works in VB.Net and it is not available in c#. 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 Jon,
This works great. This issue is resolved. Show quoteHide quote "Jon Skeet [C# MVP]" wrote: > Arne <A***@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 > Bill Butler gave you the correct solution: put the break inside your
while loop. The break will get you out of your while loop and execution will continue. There is no need to break out of the try...catch. The try...catch is irrelevant. Jon's solution, while it does work, is as he says "pretty ugly." Bruce,
You are wrong, becuase after my while loop, I have other things which I optionally don't want to execute. Show quoteHide quote "Bruce Wood" wrote: > Bill Butler gave you the correct solution: put the break inside your > while loop. The break will get you out of your while loop and execution > will continue. There is no need to break out of the try...catch. The > try...catch is irrelevant. > > Jon's solution, while it does work, is as he says "pretty ugly." > > Arne <A***@discussions.microsoft.com> wrote:
> You are wrong, becuase after my while loop, I have other things which I Then it sounds like you should either be throwing an exception, or > optionally don't want to execute. returning. Alternatively, set a flag to say whether or not to execute the other code. Either way, having an extra do/while loop won't help you. -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too Yes,
sometimes I throw exceptions to get out. It will not work at this time. Show quoteHide quote "Jon Skeet [C# MVP]" wrote: > Arne <A***@discussions.microsoft.com> wrote: > > You are wrong, becuase after my while loop, I have other things which I > > optionally don't want to execute. > > Then it sounds like you should either be throwing an exception, or > returning. Alternatively, set a flag to say whether or not to execute > the other code. Either way, having an extra do/while loop won't help > you. > > -- > Jon Skeet - <sk***@pobox.com> > http://www.pobox.com/~skeet > If replying to the group, please do not mail me too > Arne <A***@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 Jon,
A good reason? I have to claim the fifth amendment on that one. Show quoteHide quote "Jon Skeet [C# MVP]" wrote: > Arne <A***@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 > Arne <A***@discussions.microsoft.com> wrote:
> A good reason? That's fine - just don't say we didn't warn you...> I have to claim the fifth amendment on that one. -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too OK... then what you need is this:
try { bool cancel = false; while (rd.Read) { ...do a lot of stuff... if (cancel) { break; } ... do some more... } if (!cancel) { ...do yet more... } } catch (ex as Exception) { ... } finally { rd.Close(); } This expresses in code what you really want to do: "Do this stuff, but not if the user canceled." It will be easier to understand and maintain than the do...while(false) construct. Personally, I would tidy this up even more to get rid of the "break": try { bool cancel = false; while (!cancel && rd.Read) { ...do a lot of stuff... if (!cancel) { ... do some more... } } if (!cancel) { ...do yet more... } } catch (ex as Exception) { ... } finally { rd.Close(); }
Other interesting topics
include build time in exe file
exchanging data between a win32 application (service) and a c# application (service) C# Reflection - Nasty bug?? xpath in c# Control-click Start and exit applcation(s) using Windows Services deployment of dot net desktop project with crystal reports Splitting a string array to seperate variables Retrieving HTTP Response Code Form Post - Session Values |
|||||||||||||||||||||||