Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have code

C#
private string GetName()
        {
            string name = "";
            try
            {
                name = "This is my name";
                //throw new NotImplementedException();
                return "from try";
            }
            catch (Exception)
            {
                return "Error";
            }
            finally
            {
                name = "";//clears object memory.
            }
            return name;

        }


in this code if i want to return name string i can use return in the end of the function but when finally runs, it clears name value and function returns blank value.
In case i return name from try than its working fine but suppose error comes in try like i write then this try return not executed and control goes to return at the bottom which gives me blank value again.
So , please suggest which technique is best and why.
Posted
Updated 11-Jan-14 5:53am
v2

1 solution

There is no solution that allows that: the return value from the finally block will always be used, simply because it is always executed at the end of the try block regardless of what else happens within it: catch or not.

The simplest solution is just to not set name within the finally block:
C#
private string GetName()
    {
    string name = "";
    try
        {
        name = "This is my name";
        //throw new NotImplementedException();
        }
    catch (Exception)
        {
        name = "Error";
        }
    finally
        {
        }
    return name;
    }
 
Share this answer
 
Comments
Member 8128040 11-Jan-14 12:03pm    
then wat is the use of finally block.
OriginalGriff 11-Jan-14 12:08pm    
It's they to let you clean up after yourself!
Suppose you open a connection to SQL in your try block, and get an error.
You need to close the connection - but you can't just do it in the catch because you would have to duplicate code in the try block. So you do it in the finally block only, and regardless of how your code exits from the try-catch, the connection is always closed.
[no name] 11-Jan-14 13:06pm    
Solution and comment +5.
BillWoodruff 11-Jan-14 14:36pm    
Keep in mind that you cannot execute a 'return ... or any command that alters the flow-of-control ... inside a 'finally block. You can throw an Exception inside a 'finally block, however (but, why anyone would do that is beyond me).
OriginalGriff 12-Jan-14 4:30am    
So that you can report meta-failures when the "normal" system of cleaning up your mess has failed and the user data is at risk as a result.
Suppose your finally block tried to do a manual rollback on SQL and the connection had died perhaps so you didn't know what state the DB is in?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900