|
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?
|
|
|
|
|
if you exit the try block it would automatically pass through finally block no matter how it exits the try block i.e either through catch or through return statement.
have you tried to debug this. you may paste the code block if it still didnt work.
Jibesh V P
|
|
|
|
|
I am going to use a return. But this exits the whole program. So, within this condition, I am going to close the connection and have a return.
|
|
|
|
|
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
|
|
|
|
|
If the database returns 0 rows, exit else continue.
|
|
|
|
|
Ok. did I answer your question? if not can you please share the code for the better understanding of your problem.
Jibesh V P
|
|
|
|
|
Yes, I think the general concensus is that, put try-catch-finally in all thread functions as coding practice.
dev
|
|
|
|
|
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.
|
|
|
|
|
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 .
|
|
|
|
|
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.
|
|
|
|
|
Just return from the method. The finally block will execute and then your method will return.
|
|
|
|
|
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{
}
catch(SqlExcpetion sqlex){
}
catch(IOException){
}
catch(Exception ex){
}
finally{
}
- 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.
|
|
|
|
|
Just write return and the function will end after executing finally 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.
|
|
|
|
|
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 a using
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.
|
|
|
|
|
I know what happens to lock(x) in event of a Thread.Abort, what about "using"?
dev
|
|
|
|