473,320 Members | 1,945 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,320 software developers and data experts.

Code after Try...Catch Block

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.
Nov 20 '05 #1
18 2854

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

"Simon" <us****@simoneast.NOSPAMnet> wrote in message
news:0b****************************@phx.gbl...
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.

Nov 20 '05 #2
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

"Simon" <us****@simoneast.NOSPAMnet> wrote in message
news:0b****************************@phx.gbl...
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.

Nov 20 '05 #3
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
Nov 20 '05 #4
"Simon" <us****@simoneast.NOSPAMnet> schrieb
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.
--
Armin

Nov 20 '05 #5
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
"Cor" <no*@non.com> wrote in message
news:eF**************@TK2MSFTNGP12.phx.gbl...
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

Nov 20 '05 #6

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.
-----Original Message-----
Hi Cor,

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

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

Nov 20 '05 #7
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
"Simon" <us****@simoneast.NOSPAMnet> wrote in message
news:03****************************@phx.gbl...

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.
-----Original Message-----
Hi Cor,

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

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

Nov 20 '05 #8

"Simon" <us****@simoneast.NOSPAMnet> wrote in message
news:03****************************@phx.gbl...

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.
-----Original Message-----
Hi Cor,

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

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

Nov 20 '05 #9
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.

Nov 20 '05 #10
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).
"Alexandre Moura" <am****@online.microsoft.com> wrote in message
news:qE**************@cpmsftngxa06.phx.gbl...
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.

Nov 20 '05 #11
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

"Simon" <us****@simoneast.NOSPAMnet> wrote in message
news:0b****************************@phx.gbl...
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.

Nov 20 '05 #12
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

Nov 20 '05 #13
* "Scott M." <s-***@badspamsnet.net> scripsit:
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.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #14
* am****@online.microsoft.com (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.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #15
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.
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
* "Scott M." <s-***@badspamsnet.net> scripsit:
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.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Nov 20 '05 #16
* "Scott M." <s-***@badspamsnet.net> scripsit:
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...

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #17
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 ;)

--------------------
From: "Cor" <no*@non.com>
References: <0b****************************@phx.gbl> <eF**************@TK2MSFTNGP12.phx.gbl>
<ej**************@tk2msftngp13.phx.gbl>
<qE**************@cpmsftngxa06.phx.gbl>Subject: Re: Code after Try...Catch Block
Date: Sat, 25 Oct 2003 10:40:33 +0200
Lines: 11
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <Od**************@TK2MSFTNGP09.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.vb
NNTP-Posting-Host: ip3e830773.speed.planet.nl 62.131.7.115
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP09.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:150415
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

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


Nov 20 '05 #18
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: <0b****************************@phx.gbl> <eF**************@TK2MSFTNGP12.phx.gbl>
<ej**************@tk2msftngp13.phx.gbl>
<qE**************@cpmsftngxa06.phx.gbl>
<#I**************@TK2MSFTNGP12.phx.gbl>
<#S*************@TK2MSFTNGP11.phx.gbl>
<#G**************@TK2MSFTNGP11.phx.gbl>X-Face: 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#^dFrom: hi***************@gmx.at (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.1MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
Message-ID: <eV**************@TK2MSFTNGP09.phx.gbl>
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!TK2MSFTN GP09.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:150497
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

* "Scott M." <s-***@badspamsnet.net> scripsit:
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...

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>


Nov 20 '05 #19

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

Similar topics

2
by: Bret Pehrson | last post by:
Suppose the following: // Unmanaged code class UnmanagedException /* not visible outside of unmanaged code */ { }; void DoSomething() /* visible (exported) to managed code */ { throw new...
6
by: C# Learner | last post by:
In the following two code blocks, DoSomethingUseful throws AnException, while neither of the other called methods throw any exceptions. Which of the code blocks is better (in terms of readability,...
5
by: portroe | last post by:
Hi, how can I get my forms program to perform a particular action again if the incorrect format is entered, I have tried GOTO line xxx
15
by: Neo | last post by:
Hello All, Although, I have read all the advantages of using Try Catch Block instead of "On error goto", I am still confused what is alternative for classic "Resume" statement. "Resume" was one...
6
by: foolmelon | last post by:
If a childThread is in the middle of a catch block and handling an exception caught, the main thread calls childThread.Abort(). At that time a ThreadAbortException is thrown in the childThread. ...
4
by: cj | last post by:
my old code Try Dim sw As New System.io.StreamWriter(fileName, True) sw.WriteLine(strToWrite) sw.Close() Catch End Try my new code
10
by: George2 | last post by:
Hello everyone, Here is a sample from Dr. Dobb C++. In the analysis, the code is bad below. But I do not think the code is bad, 1. if bad_alloc is thrown in new int, we just catch it and...
4
by: Elmo Watson | last post by:
I've got one main method, with a Try Catch block. In that, I catch the error and put it on a label. However, inside the Try portion, I also run another method. I have a Try/Catch block there,...
5
by: vmnvmcxbv | last post by:
Why does this: try { throw 1; } catch(...) { handle_exception(); }
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.