Xiaowei.Jia
Xiaowei.Jia

Reputation: 415

Would the finally block execute when stop debugging inside try block?

I open a database connection in a try block. If I step into the try block, and then stop debugging, would the finally block be executed? In other words, does the connection remain open?

Upvotes: 4

Views: 1013

Answers (3)

Luca Ghersi
Luca Ghersi

Reputation: 3321

If by "stop debugging" you mean stopping the application of course not (for example stopping the current debug session pressing the stop button inside Visual Studio). This is the typical case for a console application launched by pressing F5 button.

If by "stopping" you mean "detaching" from currently running application launched outside VS (for example the one you attached with Ctrl+Alt+P), the program will run to it's end and will do everything is programmed for. It's also true for any web application which is running inside IIS (but not the IIS express).

Upvotes: 4

sujith karivelil
sujith karivelil

Reputation: 29006

It depends on the application; if it is web application the rest of code will execute, since it can be accessed from the generated .dll files. if it is windows application then the finally block will not be executed

Upvotes: 2

Patrick Hofman
Patrick Hofman

Reputation: 156918

No, if you terminate the app from your debugger, the finally block is not executed. The entire process is killed. Indeed, this may lead to undisposed references. (If you just detach, it keeps running)

The connection will not remain open though. The server will disconnect sooner or later.

Upvotes: 4

Related Questions