Go to finally in try block
-
what makes the program to exit? is that how it has to work?
Quote:
So, within this condition, I am going to close the connection and have a return
since you want to close the connection before the application exists thats the best place to do this reset.
Jibesh V P
-
Hi, I have two try blocks, two catches and one finally block. I have logic written in the first try block. But if one of the conditions is hit, I want the program to go to finally block directly, since I am closing all connections here, and then exit program. If I use Environment.Exit(); then it will end the program, and I would not have closed the connection to database that is open. How can I force program to go to finally block wit hexecuting the remaining code?
vanikanc wrote:
If I use Environment.Exit(); then it will end the program, and I would not have closed the connection to database that is open.
It will not end the program, but terminate it. Use Application.Terminate for a WinApp, and simply return from the Main method (the entry-point) if you're writing a Console App.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]
-
Hi, I have two try blocks, two catches and one finally block. I have logic written in the first try block. But if one of the conditions is hit, I want the program to go to finally block directly, since I am closing all connections here, and then exit program. If I use Environment.Exit(); then it will end the program, and I would not have closed the connection to database that is open. How can I force program to go to finally block wit hexecuting the remaining code?
vanikanc wrote:
two try blocks, two catches and one finally block
You'd have to show the code for us to understand how you have them arranged.
vanikanc wrote:
closed the connection to database
You should be using a
using
statement as well -- it's similar to a try/finally.vanikanc wrote:
Environment.Exit();
Don't do that; just
return
. -
Hi, I have two try blocks, two catches and one finally block. I have logic written in the first try block. But if one of the conditions is hit, I want the program to go to finally block directly, since I am closing all connections here, and then exit program. If I use Environment.Exit(); then it will end the program, and I would not have closed the connection to database that is open. How can I force program to go to finally block wit hexecuting the remaining code?
vanikanc wrote:
then it will end the program, and I would not have closed the connection to database that is open.
When the program terminates the connection will be terminated as well. And the only way you can terminate a database connection in C# anyways is if all of the following are true. 1. You have configured the connection pool to keep no idle connections. 2. There are no connections in use 3. You reset the pool. Other than that just "closing" the connection does nothing more than return it to the connection pool.
-
Hi, I have two try blocks, two catches and one finally block. I have logic written in the first try block. But if one of the conditions is hit, I want the program to go to finally block directly, since I am closing all connections here, and then exit program. If I use Environment.Exit(); then it will end the program, and I would not have closed the connection to database that is open. How can I force program to go to finally block wit hexecuting the remaining code?
Just return from the method. The finally block will execute and then your method will return.
WP Apps - Color Search | Arctic | XKCD | Sound Meter | Speed Dial
-
Hi, I have two try blocks, two catches and one finally block. I have logic written in the first try block. But if one of the conditions is hit, I want the program to go to finally block directly, since I am closing all connections here, and then exit program. If I use Environment.Exit(); then it will end the program, and I would not have closed the connection to database that is open. How can I force program to go to finally block wit hexecuting the remaining code?
Some alternate ideas. - Are the two try/catch absolutely necessary? Try/catch is an expensive operation and I see many people using it where a simple if statement could save the day. - Perhaps you can remove the second try, but move the catch to the first try. you can "assign" multiple catch handlers to one try. Of course only valid if you specify the exceptions. eg.:
try{
//code here
}
catch(SqlExcpetion sqlex){
//handle sql exceptions
}
catch(IOException){
//handle io exceptions
}
catch(Exception ex){
//handle all other exceptions
}
finally{
//handle anything that needs to be handled like freeing file handles, closing connections, ...
//make sure you test for null values etc, because otherwise you'll get an error in the finally block, which is a bit silly.
}- the quick and dirty way is maybe a goto statement, but you don't want to go that way, they're evil hope this helps.
-
Hi, I have two try blocks, two catches and one finally block. I have logic written in the first try block. But if one of the conditions is hit, I want the program to go to finally block directly, since I am closing all connections here, and then exit program. If I use Environment.Exit(); then it will end the program, and I would not have closed the connection to database that is open. How can I force program to go to finally block wit hexecuting the remaining code?
Just write
return
and the function will end after executingfinally
block.Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore, Dream. Discover.
-
Hi, I have two try blocks, two catches and one finally block. I have logic written in the first try block. But if one of the conditions is hit, I want the program to go to finally block directly, since I am closing all connections here, and then exit program. If I use Environment.Exit(); then it will end the program, and I would not have closed the connection to database that is open. How can I force program to go to finally block wit hexecuting the remaining code?
One suggestion from my side. If you put you connection inside a
using
block, you will not have to rely on any specific piece of code to close the connection and you can simply return from anywhere. It is the recommended way of doing the connection business. This article will tell you how to use the connections with ausing
ASP.NET - How To Use(Open/Close) Connections Correctly[^]Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore, Dream. Discover.
-
Ok. did I answer your question? if not can you please share the code for the better understanding of your problem.
Jibesh V P
-
One suggestion from my side. If you put you connection inside a
using
block, you will not have to rely on any specific piece of code to close the connection and you can simply return from anywhere. It is the recommended way of doing the connection business. This article will tell you how to use the connections with ausing
ASP.NET - How To Use(Open/Close) Connections Correctly[^]Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore, Dream. Discover.