none
Return Statement in Finally block RRS feed

  • Question

  • As C# is launched after java it comprises of many of the best features of java , If we look at java we can define a return statement in finally block while this feature is removed in c#.

    Why is it so?Its like a comparision between the approaches of java and C#

    Monday, September 12, 2011 3:50 AM

Answers

  • While I agree with other people who have said you can accomplish this in other ways, I don't think that's a good enough reason for it to be left out. There's a much bigger problem which hasn't been addressed yet.

    If you have a return statement in a finally block, it can lead to some really confusing situations. For example:

    try
    {
            return 10;
    }
    catch (Exception e)
    {
            return 20;
    }
    finally
    {
            return 30;
    }
    

    If this were valid code, what would the expected result be? The finally block gets executed after the first return. Does that mean the method should always return 30? This is what Java does, and a large number of Java programmers consider this a design mistake (for example, see this discussion on SO). C# avoids the confusion and all of the related issues by just not allowing overriding return statements in try/finally.


    Check out My Blog for tech news, development tips, and other information for geeks like me.
    • Proposed as answer by Albin Abel Monday, September 12, 2011 3:52 PM
    • Marked as answer by Min ZhuModerator Friday, September 16, 2011 2:58 AM
    Monday, September 12, 2011 3:25 PM
  • I think it makes sense that we don't put a return statement in the finally block.  When you consider what it's used for.  It's supposed to be the very final section that's executed after your method finishes. A return marks the finish of your method.  So it makes sense that the return should be put inside your try.  Once your program returns to the caller, then the finally block does its thing to do any cleanup after the party's over (or at least until the try and catch sections are over).  

     

     


    Tom Overton


    Monday, September 12, 2011 4:21 AM
  • I think it makes sense that we don't put a return statement in the finally block.  When you consider what it's used for.  It's supposed to be the very final section that's executed after your method finishes. A return marks the finish of your method.  So it makes sense that the return should be put inside your try.  Once your program returns to the caller, then the finally block does its thing to do any cleanup after the party's over (or at least until the try and catch sections are over).  

    @Tom - Little correction. finally executes after try block but not after method finishes.

    @Ashutosh - Why do you want return in finally. Let us see something like below where you want to return in finally (note: Below code is just for Demo purpose & it doesn't compile)

    public void SampleMethod()
    {
           try
           {
                 //some statements             
           }
           finally
           {
                 return;
           }
    
           //some other statments
    }
    

    You say, you don't want to execute 'Some other statements' after finally block and return. If so, then directly return from try block itself. As soon as return is executed inside try block, first it goes to finally and executes all code inside finally and returns. Indirectly you can say, "Having a return statement inside a try block is equivalent to having return statement as the last statement inside the finally block".

    So, you can write the above code as,

    public void SampleMethod()
    {
           try
           {
                 //some statements             
                 return; 
           }
           finally
           {
                
           }
    
           //some other statments
    }
    

    Hope you understand this.


    Please mark this post as answer if it solved your problem. Happy Programming!
    Monday, September 12, 2011 9:53 AM

All replies

  • I think it makes sense that we don't put a return statement in the finally block.  When you consider what it's used for.  It's supposed to be the very final section that's executed after your method finishes. A return marks the finish of your method.  So it makes sense that the return should be put inside your try.  Once your program returns to the caller, then the finally block does its thing to do any cleanup after the party's over (or at least until the try and catch sections are over).  

     

     


    Tom Overton


    Monday, September 12, 2011 4:21 AM
  • I think it makes sense that we don't put a return statement in the finally block.  When you consider what it's used for.  It's supposed to be the very final section that's executed after your method finishes. A return marks the finish of your method.  So it makes sense that the return should be put inside your try.  Once your program returns to the caller, then the finally block does its thing to do any cleanup after the party's over (or at least until the try and catch sections are over).  

    @Tom - Little correction. finally executes after try block but not after method finishes.

    @Ashutosh - Why do you want return in finally. Let us see something like below where you want to return in finally (note: Below code is just for Demo purpose & it doesn't compile)

    public void SampleMethod()
    {
           try
           {
                 //some statements             
           }
           finally
           {
                 return;
           }
    
           //some other statments
    }
    

    You say, you don't want to execute 'Some other statements' after finally block and return. If so, then directly return from try block itself. As soon as return is executed inside try block, first it goes to finally and executes all code inside finally and returns. Indirectly you can say, "Having a return statement inside a try block is equivalent to having return statement as the last statement inside the finally block".

    So, you can write the above code as,

    public void SampleMethod()
    {
           try
           {
                 //some statements             
                 return; 
           }
           finally
           {
                
           }
    
           //some other statments
    }
    

    Hope you understand this.


    Please mark this post as answer if it solved your problem. Happy Programming!
    Monday, September 12, 2011 9:53 AM
  • @Tom - Little correction. finally executes after try block but not after method finishes.

    Actually what I meant was it executes after the method returns if there is a return in the try block.  A lot of people are confused about that, they think  they can set a return value in the finally block and it will return that value to the caller but it won't , the program will return (which is what I was meaning by method "finishing" because to the caller, the method is finished after the return) and then the finally block will get run.
    Tom Overton
    Monday, September 12, 2011 12:23 PM
  • In all of my years of programming I never thought to put a return statement in a finally block. That so breaks the paradigm of a finally block.

    Why would you put a return statement in a finally block? Is there something you can do with that, that you can do in another way?

    Monday, September 12, 2011 2:40 PM
  • While I agree with other people who have said you can accomplish this in other ways, I don't think that's a good enough reason for it to be left out. There's a much bigger problem which hasn't been addressed yet.

    If you have a return statement in a finally block, it can lead to some really confusing situations. For example:

    try
    {
            return 10;
    }
    catch (Exception e)
    {
            return 20;
    }
    finally
    {
            return 30;
    }
    

    If this were valid code, what would the expected result be? The finally block gets executed after the first return. Does that mean the method should always return 30? This is what Java does, and a large number of Java programmers consider this a design mistake (for example, see this discussion on SO). C# avoids the confusion and all of the related issues by just not allowing overriding return statements in try/finally.


    Check out My Blog for tech news, development tips, and other information for geeks like me.
    • Proposed as answer by Albin Abel Monday, September 12, 2011 3:52 PM
    • Marked as answer by Min ZhuModerator Friday, September 16, 2011 2:58 AM
    Monday, September 12, 2011 3:25 PM