Break out of Try

  • Thread starter Thread starter Guest
  • Start date Start date
Jon,
This works great. This issue is resolved.

Jon Skeet said:
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.
 
Arne said:
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.
 
Arne said:
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
 
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."
 
Bill,
That will not work because after my while loop I have other stuff that I
optionally do not want to executed.
 
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.
 
Bruce,
You are wrong, becuase after my while loop, I have other things which I
optionally don't want to execute.
 
Arne said:
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.
 
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();
}
 
Arne said:
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
 
Arne said:
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.
 
Back
Top