473,625 Members | 2,733 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2878

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.causeanexcep tion ()
do.thiswontexec uteifanexceptio nfired ()

catch ( Ex as Exception )
do.therewasanex ception()
finally
do.thisisalways executed ()
end try

"Simon" <us****@simonea st.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.causeanexcep tion ()
do.thiswontexec uteifanexceptio nfired ()

catch ( Ex as Exception )
do.therewasanex ception()
finally
do.thisisalways executed ()
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
"therewasanexce ption" or "thisisalwaysex ecuted" 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****@simonea st.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........Catc h 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****@simonea st.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.Reflecti on Master "

==== Converting to 2002 ====
Remove inline declarations
"Cor" <no*@non.com> wrote in message
news:eF******** ******@TK2MSFTN GP12.phx.gbl...
Hi Simon,

I had always the idea that code was executed in a Try........Catc h 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...E nd 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...E nd 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.Reflecti on Master "

==== Converting to 2002 ====
Remove inline declarations
"Simon" <us****@simonea st.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****@simonea st.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

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

Similar topics

2
2589
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 UnmangedException(); }
6
1491
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, maintainability, etc.), in your opinions? A { Start(); try {
5
2696
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
11752
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 of the crucial line in debugging which let VB programmers go to exact line where error was thrown. I still use on error goto instead of try catch since, I want "Resume" for debugging. Is there any alternative like Resume in VB.net for Try
6
7438
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. The question is: after the ThreadAbortException is thrown, does the childThread continue run the remaining code in the catch block?
4
8912
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
1626
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 write some log;
4
1610
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, also, except I just have 'Throw' in the Catch portion. How can I send something from this 'internal' method, to the 'outer' method, so I can see where the actual failure is coming from?
5
1421
by: vmnvmcxbv | last post by:
Why does this: try { throw 1; } catch(...) { handle_exception(); }
0
8189
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8635
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8354
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8497
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6116
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4089
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2621
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1499
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.