C# return doesn't hit the Finally block

Hi, I'm using VS2013, c# and asp.net webform
In a couple of my methods, I use "return" in the middle of the code when certain conditions are met.  To my surprise, my finally block code is not triggered when return is hit and it just leaves the method.  Is there a way to make it go to finally block before leaving the method/

Thank you.
lapuccaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi lapucca;

Can you post your code please.
0
it_saigeDeveloperCommented:
In the .NET logic flow; a finally block is *always* executed.

There are only four situations that I can think of where a finally block will *never* be executed:
1.  System.Exit() or Environment.Exit() is called from the try block.
using System;

namespace EE_Q28712880
{
	class Program
	{
		static void Main(string[] args)
		{
			try
			{
				Console.WriteLine("In try...");
				Environment.Exit(0);
			}
			catch (Exception)
			{
				Console.WriteLine("In catch...");
			}
			finally
			{
				Console.WriteLine("In finally...");
			}
			Console.ReadLine();
		}
	}
}

Open in new window

2.  System crashes or power is lost while executing the try block.
3.  There is an infinite loop inside the try block.
4.  An exception that cannot be handled is thrown; e.g. - StackOverflowException.
In the .NET Framework 1.0 and 1.1, you could catch a StackOverflowException object (for example, to recover from unbounded recursion). Starting with the .NET Framework 2.0, you can’t catch a StackOverflowException object with a try/catch block, and the corresponding process is terminated by default. Consequently, you should write your code to detect and prevent a stack overflow.
- Source

Could you provide an example from your code that is not indicated by one of the previous examples, where the finally block is not executed?

-saige-
2
AndyAinscowFreelance programmer / ConsultantCommented:
>>my finally block code is not triggered when return is hit and it just leaves the method.

How do you test that - or is that an assumption based on something else?
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

MlandaTCommented:
The culprit is Environment.Exit Especially considering that there has not been reports of other exceptions (StackOverflow) or system hanging (infinite loop).

MSDN: https://msdn.microsoft.com/en-us/library/system.environment.exit(v=vs.110).aspx
Terminates this process and returns an exit code to the operating system. If Exit is called from a try or catch block, the code in any finally block does not execute. If the return statement is used, the code in the finally block does execute.
0
lapuccaAuthor Commented:
Okay, I stepped through in Debug mode after a return statement the finally block wasn't hit.  But now, I'm thinking is it because the return statement was outside of the try block?  If the return is hit before try, catch and finally blocks then the finally doesn't get hit at all, right?
0
it_saigeDeveloperCommented:
Correct.  In the following code, the try...catch...finally block is not executed as the return statement would render it unreachable:
using System;

namespace EE_Q28712880
{
	class Program
	{
		static void Main(string[] args)
		{
			return;
			try
			{
				Console.WriteLine("In try...");
				Environment.Exit(0);
			}
			catch (Exception)
			{
				Console.WriteLine("In catch...");
			}
			finally
			{
				Console.WriteLine("In finally...");
			}
			Console.ReadLine();
		}
	}
}

Open in new window

In this case, though, the IDE should give you a warning (the code will still compile) -Capture.JPGCapture.JPG-saige-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
Can you please post the actual code so we have a better idea of what is happening.
0
lapuccaAuthor Commented:
Sorry, I didn't post the code because too much sensitive code that I would have to comment out.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.