Lee
Lee

Reputation: 1641

Is a finally block guaranteed to be executed for an unhandled exception in asp.net?

In this question When is finally run if you throw an exception from the catch block?

It explains the caveat that the finally-block is guaranteed to run unless the exception is unhandled, as confirmed by this documentation.

For a console app, it's simple, an unhandled exception kills the process. However, in ASP.net my understanding becomes muddied. An unhandled exception in ASP.net doesn't kill the process or even recycle the app pool, in-fact framework code executes so that the error details are sent back over the wire. However, such errors still commonly referred to as unhandled exceptions.

As far as the .net runtime is concerned, is such an "unhandled exception" in ASP.net actually handled, so that I can trust the finally-block will always be executed?

Note: I understand that some exceptions such as a StackOverflowException will always crash the app pool, and thus likely not execute the finally-block, I'm not concerned about those edge cases.

Upvotes: 2

Views: 297

Answers (1)

Philip Daubmeier
Philip Daubmeier

Reputation: 14934

As the exception is handled further up the stack by the asp.net framework, the finally block will be executed. Only because your code doesnt handle the exception it doesnt mean it is unhandled.

Upvotes: 1

Related Questions