Click here to Skip to main content
15,881,838 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Why When you try to return value from Finally block you will get error "Control cannot leave the body of a finally clause
C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine( f1());
Console.ReadKey();
}


static int f1()
{
try
{
return 10;
}
finally
{
return 5; // This is where Error occurs
}
}
}
}
Posted
Updated 30-Jan-19 19:50pm
Comments
BillWoodruff 15-Sep-14 4:10am    
There are a lot of "why" questions about how things work in .NET that can only be answered with the statement: because the language was designed that way.

I'd like to suggest you focus on understanding the try/catch/finally structure as a "whole," rather than focusing on the "pieces"of it. There's little point in using try/finally without a "catch." Understanding how to throw your own Exception (in the "catch"), and understanding of what Exceptions are is very important. cheers, Bill

You have to write code as :

Just simple code for returns value with finally block... As finally used for cleanup resources...
if any error occurs then catch will execute and set the return value...


SQL
public  bool func_a()
   {
     bool bFlag = true;
     try
        {
          SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["Connection"].ConnectionString);
          con.Open();
         // do whatever...
           bFlag = true;
        }
        catch (Exception ex)
        {
            bFlag = false;
        }
        finally
        {
            con.Close();
        }
        return bFlag;


"As finally used for cleanup resources so that the control cant be exit without its completion (cleanup all resources)... "

And as the finally block will always executes whatever condition error occurs or not So we cant place two return statement in a flow... as you used in try block and then in finally block.(if no error occurred then your try code will execute then finally..so how can two return statement will works... )

--------------
It is a compile-time error for a break, continue, or goto statement to transfer control out of a finally block. When a break, continue, or goto statement occurs in a finally block, the target of the statement must be within the same finally block, or otherwise a compile-time error occurs.
From :
http://msdn.microsoft.com/en-us/library/aa664733%28v=vs.71%29.aspx[^]
:-)
 
Share this answer
 
v5
Comments
Suvabrata Roy 15-Sep-14 2:45am    
First read the question then answer he/she asked why not how.
Hemant Singh Rautela 15-Sep-14 2:55am    
I did both.... :-) just read the complete answer (As finally used for cleanup resources)
and give solution to him how it will works...

yes I missed one more line for finally : "as finally used for cleanup resources so that the control cant be exit without its completion.. "
I will update it...
Suvabrata Roy 15-Sep-14 3:00am    
1. Its a common practice to open a Connection while writing any function?
2. Before using any variable in Finally block check its reference or value it may be a case that variable might not be initialized.
BillWoodruff 15-Sep-14 4:06am    
I think this answer deserves a #4: the author shows a code example using try/catch, and the explanation is reasonable.
Hemant Singh Rautela 15-Sep-14 4:11am    
Vote it with 5... :-) (I didn't open that link)
Hi,

Read about Finally : http://msdn.microsoft.com/en-IN/library/zwc8s4fz.aspx[^]

Now finally block is used to cleanup your resources which used or initialized in try block, finally is a must run block whatever code is there is should execute every time try has been executed ( If exception occurs then also) so if you return from finally block it will break the execution flow and application may misbehave.

If I did not write any Catch block and Exception occur ?

Still finally block will be executed but it have some dependency on your Machine's CLR settings. For more info : http://msdn.microsoft.com/en-us/magazine/cc793966.aspx[^]


Thanks,
Suvabrata
 
Share this answer
 
error is that your function is returning two values in try and in final block.for further se
See this for your reference[^]
 
Share this answer
 
I think here you may be trying to use the finally block in an odd way. The Framework documentation typifies finally as follows:

The finally block is useful for cleaning up any resources allocated in the try block.


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 5?
 
Share this answer
 
v2

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