Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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?

like image 471
Xiaowei.Jia Avatar asked Oct 21 '25 02:10

Xiaowei.Jia


2 Answers

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).

like image 146
Luca Ghersi Avatar answered Oct 23 '25 15:10

Luca Ghersi


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.

like image 34
Patrick Hofman Avatar answered Oct 23 '25 17:10

Patrick Hofman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!