473,320 Members | 2,052 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.

Try.. Catch....

I am using VB.Net with .Net Framework 1.1

Here is my code

Try

'Try my statements

Catch

'Catch my exception

Exit Sub

Finally

'Close my database connections...

What I noticed is when the program flow enters the Catch block, even though
there is an Exit Sub statement here, program still executes "Finally"
statement.

Here are my questions.

1.. There is no effect of exit statement here. Finally will be always
called when finally block statement exist.
2.. Finally block will be executed irrespective the catch block is
executed or not.
Is it correct? Any thoughts?

Neelam
Nov 18 '05 #1
5 1653
The Finally block is always executed. Period. The reason for this is, as you
have illustrated, certain things need to be done regardless of an exception
occurring, such as closing files and disposing database objects (among many
others). Putting a piece of code in the Finally block ensures that that code
will be executed, even if an exception occurs.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living

"Neelam Karne" <Ne*********@hotmail.com> wrote in message
news:eg*************@TK2MSFTNGP11.phx.gbl...
I am using VB.Net with .Net Framework 1.1

Here is my code

Try

'Try my statements

Catch

'Catch my exception

Exit Sub

Finally

'Close my database connections...

What I noticed is when the program flow enters the Catch block, even though there is an Exit Sub statement here, program still executes "Finally"
statement.

Here are my questions.

1.. There is no effect of exit statement here. Finally will be always
called when finally block statement exist.
2.. Finally block will be executed irrespective the catch block is
executed or not.
Is it correct? Any thoughts?

Neelam

Nov 18 '05 #2
Finally always executes when a try is initiated if you declare finally. You
cant stop this - control is always passed to the finally block regardless of
how the try block exits. Its the best place for cleanup.

--
Regards

John Timney
Microsoft Regional Director
Microsoft MVP
"Neelam Karne" <Ne*********@hotmail.com> wrote in message
news:eg*************@TK2MSFTNGP11.phx.gbl...
I am using VB.Net with .Net Framework 1.1

Here is my code

Try

'Try my statements

Catch

'Catch my exception

Exit Sub

Finally

'Close my database connections...

What I noticed is when the program flow enters the Catch block, even though there is an Exit Sub statement here, program still executes "Finally"
statement.

Here are my questions.

1.. There is no effect of exit statement here. Finally will be always
called when finally block statement exist.
2.. Finally block will be executed irrespective the catch block is
executed or not.
Is it correct? Any thoughts?

Neelam

Nov 18 '05 #3
Yes, you're right.
The finally block will always be executed whether there was an error or not.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
"Neelam Karne" <Ne*********@hotmail.com> wrote in message
news:eg*************@TK2MSFTNGP11.phx.gbl...
I am using VB.Net with .Net Framework 1.1

Here is my code

Try

'Try my statements

Catch

'Catch my exception

Exit Sub

Finally

'Close my database connections...

What I noticed is when the program flow enters the Catch block, even
though
there is an Exit Sub statement here, program still executes "Finally"
statement.

Here are my questions.

1.. There is no effect of exit statement here. Finally will be always
called when finally block statement exist.
2.. Finally block will be executed irrespective the catch block is
executed or not.
Is it correct? Any thoughts?

Neelam

Nov 18 '05 #4
Like you have it - in the Finally section - you want it to ALWAYS, finally
close the connection, even if the coding failed - -if it exited before
then, and the FINALLY section wasn't 'fired', then, your db connections
would remain open (something that you definitely wouldn't want).
Why not, if you need the 'Exit Sub' line - - put that at the end of your
FINALLY section.

David Wier
MCP, MVP ASP.NET, ASPInsider
http://aspnet101.com
http://aspexpress.com
"Neelam Karne" <Ne*********@hotmail.com> wrote in message
news:eg*************@TK2MSFTNGP11.phx.gbl...
I am using VB.Net with .Net Framework 1.1

Here is my code

Try

'Try my statements

Catch

'Catch my exception

Exit Sub

Finally

'Close my database connections...

What I noticed is when the program flow enters the Catch block, even though there is an Exit Sub statement here, program still executes "Finally"
statement.

Here are my questions.

1.. There is no effect of exit statement here. Finally will be always
called when finally block statement exist.
2.. Finally block will be executed irrespective the catch block is
executed or not.
Is it correct? Any thoughts?

Neelam

Nov 18 '05 #5
What good would putting an "Exit Sub" statement at the end of the Finally
block do? That's kind of like telling someone to leave as they're walking
out the door! Oh, by the way, on your way out, would you mind leaving?

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living

"David Wier" <dw***@nospamASPNet101.com> wrote in message
news:uH**************@TK2MSFTNGP11.phx.gbl...
Like you have it - in the Finally section - you want it to ALWAYS, finally
close the connection, even if the coding failed - -if it exited before
then, and the FINALLY section wasn't 'fired', then, your db connections
would remain open (something that you definitely wouldn't want).
Why not, if you need the 'Exit Sub' line - - put that at the end of your
FINALLY section.

David Wier
MCP, MVP ASP.NET, ASPInsider
http://aspnet101.com
http://aspexpress.com
"Neelam Karne" <Ne*********@hotmail.com> wrote in message
news:eg*************@TK2MSFTNGP11.phx.gbl...
I am using VB.Net with .Net Framework 1.1

Here is my code

Try

'Try my statements

Catch

'Catch my exception

Exit Sub

Finally

'Close my database connections...

What I noticed is when the program flow enters the Catch block, even

though
there is an Exit Sub statement here, program still executes "Finally"
statement.

Here are my questions.

1.. There is no effect of exit statement here. Finally will be always
called when finally block statement exist.
2.. Finally block will be executed irrespective the catch block is
executed or not.
Is it correct? Any thoughts?

Neelam


Nov 18 '05 #6

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

Similar topics

10
by: Gary.Hu | last post by:
I was trying to catch the Arithmetic exception, unsuccessfully. try{ int a = 0, b = 9; b = b / a; }catch(...){ cout << "arithmetic exception was catched!" << endl; } After ran the program,...
11
by: kaeli | last post by:
Hey all, I'd like to start using the try/catch construct in some scripts. Older browsers don't support this. What's the best way to test for support for this construct so it doesn't kill...
4
by: Abhishek Srivastava | last post by:
Hello All, I have seen code snippets like try { ..... } catch {
11
by: Pohihihi | last post by:
I was wondering what is the ill effect of using try catch in the code, both nested and simple big one. e.g. try { \\ whole app code goes here } catch (Exception ee) {}
13
by: Benny | last post by:
Hi, I have something like this: try { // some code } catch // note - i am catching everything now {
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
32
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I...
23
by: pigeonrandle | last post by:
Hi, Does this bit of code represent complete overkill?! try { //create a treenode TreeNode tn = new TreeNode(); //add it to a treeview tv.Nodes.Add(tn);
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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...
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...
1
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....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.