Return-Try-Catch-Finally Dilemma - C#

Although it is a simple part of the programming world, a lot of people ask, ' re curious about, wonder, finds an answer but may forget again while coding; how should I behave with the function with a return while having try-catch-finally blocks, where to put return ? where should the finally stand ? how to manage a possible error-handling in the process ?

[Bu içeriği Türkçe blogumda okumak için buraya tıklayınız.]

You may come across a lot of articles, sources at StackOverFlow type resources or at MSD articles, various programmers blogs.

So I wanted to share some scenarious about that case.

Actually the ideal behaviour and the flow is clear about this, what really matters is that how we should manage the error-handling situation and what our function should return at the error time.
In order to decide this we should know how the process behaves over dotnet framework architecture. Let' s simulate the process with a few scenarios.



The Ideal Format;

our function
   {
        setting a variable to our return variable something goes wrong;
              try
                   {
                        our process and calculating our return value;
                    }
               catch
                     {
                           ...
                      }
                finally
                      {
                           do sth whenever it is ok or not;
                       }
              our return value;
    }

NOTE :

1- Return can not stand behind finally block, after catch block
2- Even if we dont have Finally block, return cant stay after catch block,
3- If we dont have a return syntax after Finally, Catch blocks,  we cant have return syntax again in try block.
4- But there is an alternative usage what is not different then the format above;


our function
   {
       
              try
                   {
                       return syntax :  our process and calculating our return value;
                    }
               catch
                     {
                           ...
                      }
                finally
                      {
                           do sth whenever it is ok or not;
                       }
            return syntax :  setting a variable to our return variable something goes wrong;
    }


Let's simulate it with a few scenarios;

We have a function which takes 2 numeric parameteres and returns the calculated value.


Scenario-1 : Flow without error

                   Scenario-1 : Flow without error

As you can see first finally block executes then it returns.

   Scenario-1 : Flow without error

 Scenario 2 : Triggering an intentional error - after calculating the return value, in the try block -

 Scenario 2 : Triggering an intentional error - after calculating the return value, in the try block -

At this scenario, our calculation what sets the return value works correctly, then we re triggering an inventional error, first error message comes up then finally executes then return value prints correctly because we could set it before the error.

 Scenario 2 : Triggering an intentional error - after calculating the return value, in the try block -



 Senaryo 3 : Triggering an intentional error - before calculating the return value, in the try block -

If we trigger the inventional error ( throw new exception; )  before calculating the return value, first error message prints then finally block executes and then it prints our error time value what we did set 0 in this scenerio.


Scenario 3 : Triggering an intentional error - before calculating the return value, in the try block -


Scenario 4 : Usual error during the process. Let's change our calculation function to a division function and trigger an error by simulating a scenario where 0 divides 3.

Scenario 4 : Usual error during the process.

As you may guess; first error message triggers then finally block executes and it return 0. So I m managing an error case, I say return me 0 when things go wrong.

Last Words : Finally always executes first after trying to do something !

Finally block always executes first after trying to do something whenever it goes OK or WRONG. So we should manage our steps that we want to do whenever it goes ok or not, here. Such as; finalizing database connections, calling the GargabeCollector method..etc

It is rather critical to decide and design what to return in an error\or error-free case and we should design it like the format above.


References :

1- MSDN Article-1
2- MSDN Article-2
3-StackOverFlow Sample Case

4 comments:

© 2011-2015 | EmreCiftci.net. Powered by Blogger.