Code after Try...Catch Block

S

Simon

I was of the impression that code placed after a
Try...Catch block was only executed if there was no
exception thrown.

I've got some VB.net code as part of a Windows form that
executes even when an exception is thrown - it behaves as
if the code is part of a finally block.

Looking through all the documentation and MSDN articles,
it seems that none of the examples contain code placed
after a try...catch block nor explain the ramifications
of doing so.

Any ideas? Perhaps I should just move the code into the
try block after the code likely to cause an error.

Simon.
 
R

Robin Tucker

If you don't want it executed after an exception, put it into the try/catch
block. I don't know if code execution after the "finally" block is well
defined or not. I guess it depends on the exception.

try

do.causeanexception ()
do.thiswontexecuteifanexceptionfired ()

catch ( Ex as Exception )
do.therewasanexception()
finally
do.thisisalwaysexecuted ()
end try
 
J

Jay B. Harlow [MVP - Outlook]

Simon,
In addition to what Robin stated:

Is your code outside of the try Catch block?

Then yes it will be executed.

' code here will be executed
try

do.causeanexception ()
do.thiswontexecuteifanexceptionfired ()

catch ( Ex as Exception )
do.therewasanexception()
finally
do.thisisalwaysexecuted ()
end try
' code here will also be executed,
' unless you have a throw in your catch or finally block above.

The code after the Try block will be executed in the above case if
"therewasanexception" or "thisisalwaysexecuted" functions do not raise an
exception. If either of the functions raise an exception, then the code
after the Try block will not be executed.

Hope this helps
Jay
 
A

Armin Zingler

Simon said:
I was of the impression that code placed after a
Try...Catch block was only executed if there was no
exception thrown.

I've got some VB.net code as part of a Windows form that
executes even when an exception is thrown - it behaves as
if the code is part of a finally block.

Looking through all the documentation and MSDN articles,
it seems that none of the examples contain code placed
after a try...catch block nor explain the ramifications
of doing so.

Any ideas? Perhaps I should just move the code into the
try block after the code likely to cause an error.


You mean the code after the "End Try"? It is only executed whenever no
exception occured OrElse the occured exception is handled by a catch
statement.
 
C

Cor

Hi Simon,

I had always the idea that code was executed in a Try........Catch block,
till the exception was thrown, then went to the Catch block and then to the
Finaly block. And when there is no return or something in that, do the
statement direct after the Finaly block.

That is what it does in my debugger and if it would not be the same in a
real situation I think I would not be happy.

But if I am wrong, please correct me?

Cor
 
T

Tom Spink

Hi Cor,

Code in a finally block is executed ALWAYS, regardless of Exit
Sub/Function's or Returns. Code after a Try..End Try block is always
executed, unless there is a Exit Sub/Function or Return or another exception
thrown in the finally or catch block.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 
S

Simon

Thanks Tom and others. That clears up my thinking quite
a bit. So basically code placed after an End Try will
execute unless:
(a) the particular exception wasn't caught
(b) a further exception was thrown in the catch
(c) a return or exit sub was executed.

Whereas a finally block will execute regardless of the
above three conditions, right?

Any corrections on that? Otherwise, thanks for your help.

Simon.
 
T

Tom Spink

That's right. The *only* case where a Finally block won't execute is if the
Try...Catch...End Try block isn't entered...

' Some Code
Return
' Some Code
Try
...
Catch Foo As Bar
...
Finally
...
End Try

In this case, Return has been called before the Try...Catch...End Try block
has been reached, so finally will not execute.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations



Thanks Tom and others. That clears up my thinking quite
a bit. So basically code placed after an End Try will
execute unless:
(a) the particular exception wasn't caught
(b) a further exception was thrown in the catch
(c) a return or exit sub was executed.

Whereas a finally block will execute regardless of the
above three conditions, right?

Any corrections on that? Otherwise, thanks for your help.

Simon.
 
S

Scott M.

Thanks Tom and others. That clears up my thinking quite
a bit. So basically code placed after an End Try will
execute unless:
(a) the particular exception wasn't caught
(b) a further exception was thrown in the catch

(c) a return or exit sub was executed.

---> If you exit the try...end try, code after the try block will run. But
you are right that if you exit the sub, it won't.


Whereas a finally block will execute regardless of the
above three conditions, right?

Any corrections on that? Otherwise, thanks for your help.

Simon.
 
A

Alexandre Moura

Just being picky, since there is an exception to this rule -
if an "End" instruction is hit, execution stops at that point without
executing any finalize statements.
 
S

Scott M.

No, that would be if an "Exit Try" gets hit, it will get out of the whole
Try...End Try (without running any Finally code) and continue to whatever
code is after the End Try.

Remember, "End" is used as the natural ending point for a code block
(Sub/End Sub, Select/End Select, If/End If). "Exit" is used to prematurely
exit a code block (Exit Sub, Exit For, Exit Try).
 
R

Russ Bishop

Put "Exit Sub" statements at the end of your Catch blocks. Problem solved.

Try
'do stuff
Catch ex as Exception
'more stuff
Exit Sub
End Try
 
C

Cor

Hi Alexandre

As far as I know is it better not to use the single "end".
It does not stop your program but kills it.

And therefore the finally statement will never be executed anymore after it.

Cor
 
H

Herfried K. Wagner [MVP]

* "Scott M. said:
No, that would be if an "Exit Try" gets hit, it will get out of the whole
Try...End Try (without running any Finally code) and continue to whatever
code is after the End Try.

Remember, "End" is used as the natural ending point for a code block
(Sub/End Sub, Select/End Select, If/End If). "Exit" is used to prematurely
exit a code block (Exit Sub, Exit For, Exit Try).

No. There is an 'End' command that will quit the application.
 
H

Herfried K. Wagner [MVP]

* (e-mail address removed) (Alexandre Moura) scripsit:
Just being picky, since there is an exception to this rule -
if an "End" instruction is hit, execution stops at that point without
executing any finalize statements.

You can use 'Exit Try' to exit from a 'Try...Catch' block.
 
S

Scott M.

Yes, you're right, but based on the thread, I suspected Alexandre was
referring to an "End" of a block instruction and not THE "End" instruction.
He might of meant just "End", in which case he and you are correct.
 
H

Herfried K. Wagner [MVP]

* "Scott M. said:
Yes, you're right, but based on the thread, I suspected Alexandre was
referring to an "End" of a block instruction and not THE "End" instruction.
He might of meant just "End", in which case he and you are correct.

Sorry, for some reason I was not able to find the other messages on my
news server...
 
A

Alexandre Moura

Yes, I do believe you are correct - I was just warning that there are
situations where a finally won't get executed - by using end, for example
(pulling the plug would be another one :) )

As I said, I was being a bit picky - as a tester you tend to develop a
distaste for words like always or never, unless you are refering to a
shipping date being pushed back ;)

--------------------
<[email protected]>
 
A

Alexandre Moura

Just to clarify, I did mean the End keyword, and I was being picky - I'm
not advocating its use, just reminding people that in certain circumstances
programs may exit/stop without executing a finalize - an end keyword or the
whole process being externally terminated comes to mind.

Note that you probably shouldn't have to worry about this - it belong to
the category of "the plug was pulled from the machine" situations.

--------------------
Sender: Standardbenutzer@TU-K3YVE0N56PSA
Subject: OT: Re: Code after Try...Catch Block
References: <[email protected]> <[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
vJn^g[Lkg9YfJ,Oj#{Y[')WBo<1kS#Rc3Vb!D;jf$;OZ%<"'z+DX"K/m)h\Gi;e-AYsc%'CmL~Ix
@YEq$}A>^]KbF1.Z|=/'*CcB[f+8(m&vP.u4+P.q$n]?[s>nnFu/8EuC?h[c\#wR{ok_um~57to=
P=1"{qO1e%A2~nS?<[o`jn?C/-}7Mbz~L)WI=5VL!*xU#^d
From: (e-mail address removed) (Herfried K. Wagner [MVP])
Date: 25 Oct 2003 17:54:58 +0200
Lines: 11
User-Agent: Gnus/5.0808 (Gnus v5.8.8) XEmacs/21.4 (Common Lisp (Windows)) Hamster/2.0.0.1
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.vb
NNTP-Posting-Host: n636p031.adsl.highway.telekom.at 62.47.23.127
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:150497
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

* "Scott M. said:
Yes, you're right, but based on the thread, I suspected Alexandre was
referring to an "End" of a block instruction and not THE "End" instruction.
He might of meant just "End", in which case he and you are correct.

Sorry, for some reason I was not able to find the other messages on my
news server...
 

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

Top