Home All Groups Group Topic Archive Search About
Author
4 Apr 2005 4:40 PM
Arne
How can I break out of a try clause?
In vb.net I can do 'Exit try'. How do I do that in C#?

Author
4 Apr 2005 4:56 PM
Samuel R. Neff
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?
>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.
Are all your drivers up to date? click for free checkup

Author
4 Apr 2005 6:00 PM
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

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#?
Author
4 Apr 2005 8:33 PM
Arne
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#?
>
>
>
Author
4 Apr 2005 8:46 PM
James Curran
"Arne" <A***@discussions.microsoft.com> wrote in message
news:36ABAA5C-111C-4186-891F-CDC7A720594D@microsoft.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.
Author
4 Apr 2005 6:01 PM
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();
}
Author
4 Apr 2005 6:50 PM
Jon Skeet [C# MVP]
Chris Dunaway <dunaw***@gmail.com> wrote:
Show quoteHide quote
> 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
Author
4 Apr 2005 8:23 PM
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!
Author
4 Apr 2005 8:26 PM
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?
Author
4 Apr 2005 8:35 PM
Arne
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?
>
>
Author
4 Apr 2005 9:18 PM
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. :)
Author
4 Apr 2005 11:29 PM
Arne
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. :)
>
>
Author
4 Apr 2005 11:37 PM
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. :)
Author
5 Apr 2005 3:21 PM
Arne
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. :)
>
>
Author
5 Apr 2005 4:01 AM
MuZZy
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
Author
5 Apr 2005 3:29 PM
Arne
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
>
Author
5 Apr 2005 4:21 PM
Bill Butler
Show quote Hide quote
"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
Author
5 Apr 2005 6:07 PM
Arne
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
>
>
>
Author
5 Apr 2005 7:04 PM
Bill Butler
"Arne" <A***@discussions.microsoft.com> wrote in message
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.
>

Hi Arne,

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
Author
5 Apr 2005 8:00 AM
Evert Timmer
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...".
Author
5 Apr 2005 3:29 PM
Arne
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...".
>
Author
5 Apr 2005 4:08 PM
James Curran
"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.
Author
5 Apr 2005 6:09 PM
Arne
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.
>
>
>
Author
5 Apr 2005 7:59 AM
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
Author
5 Apr 2005 6:27 AM
Jon Skeet [C# MVP]
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
Author
5 Apr 2005 4:03 PM
Arne
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
>
Author
5 Apr 2005 5:26 PM
Bruce Wood
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."
Author
5 Apr 2005 6:09 PM
Arne
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."
>
>
Author
5 Apr 2005 6:17 PM
Jon Skeet [C# MVP]
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
Author
5 Apr 2005 6:53 PM
Arne
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
>
Author
5 Apr 2005 7:06 PM
Jon Skeet [C# MVP]
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
Author
7 Apr 2005 12:19 PM
Arne
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
>
Author
7 Apr 2005 6:10 PM
Jon Skeet [C# MVP]
Arne <A***@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
Author
5 Apr 2005 6:20 PM
Bruce Wood
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();
}

Bookmark and Share