Behaviour of Return Statement in Catch and Finally

Behaviour of return statement in catch and finally

It is overridden by the one in finally, because finally is executed after everything else.

That's why, a rule of thumb - never return from finally. Eclipse, for example, shows a warnings for that snippet: "finally block does not complete normally"

behaviour of try catch and finally with return statement workflow in c#

Finally statements are always executed, even if no exception occurs.

http://msdn.microsoft.com/en-gb/library/zwc8s4fz(v=vs.110).aspx:

Typically, the statements of a finally block run when control leaves a try statement. The transfer of control can occur as a result of normal execution, of execution of a break, continue, goto, or return statement, or of propagation of an exception out of the try statement.

Where to put return statement in try-catch or finally block

finally won't allow you to add return statement. Take a look at this to see why it is not legal to add control transfer statements in finally

You'll have to either put it outside of try/catch/finally completely or in both try and catch blocks else compiler won't be happy to compile.

public DataTable City_Name_get()
{
try
{
string store_pro = "sp_tb_city";
obj_DB_Con.connection_db(store_pro, null);
return obj_dt;
}
catch (Exception ee)
{
MessageBox.Show(ee.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;//Or whatever
}
}

Or

public DataTable City_Name_get()
{
try
{
//Do something
}
catch (Exception ee)
{
//Do something more
}
finally
{
//Do something more
}
return someDataTable;
}

Why does a return in `finally` override `try`?

Finally always executes. That's what it's for, which means its return value gets used in your case.

You'll want to change your code so it's more like this:

function example() { 
var returnState = false; // initialization value is really up to the design
try {
returnState = true;
}
catch {
returnState = false;
}
finally {
return returnState;
}
}

Generally speaking you never want to have more than one return statement in a function, things like this are why.

return in try-catch's finally block in java. Is there any good point in this example?

Is there really any point in adding a finally block here?

The answer to this is a resounding "no": putting a return statement in the finally block is a very bad idea.

I just add the return null inside the catch block, which would execute the same behavior, or am I wrong?

It wouldn't match the original behavior, but that's a good thing, because it would fix it. Rather than returning null unconditionally the way the original code does, the code with the return inside the catch block would return null only on errors. In other words, the value returned in the try branch would be returned to the caller unless there is an exception.

Moreover, if you add return null after the catch block, you would see the correct effect of returning null on exception. I would go even further, and put a single return in the method, like this:

response or = null;
try {
or = //gives it a value
log.info(or.toString());
} catch ( Exception e) {
e.printStackTrace();
}
return or;

Weird Try-Except-Else-Finally behavior with Return statements

Because finally statements are guaranteed to be executed (well, presuming no power outage or anything outside of Python's control). This means that before the function can return, it must run the finally block, which returns a different value.

The Python docs state:

When a return, break or continue statement is executed in the try suite of a try…finally statement, the finally clause is also executed ‘on the way out.’

The return value of a function is determined by the last return statement executed. Since the finally clause always executes, a return statement executed in the finally clause will always be the last one executed:

This means that when you try to return, the finally block is called, returning it's value, rather than the one that you would have had.

Will modifications to a function's return value in the finally block take effect?

The following things will happen when you will call this method:

  1. The expression, x = x + 1; will increase the value of x by 1 i.e. its value will become 1. Then, throw new Exception(); will be executed and as a result, the control will enter the catch block.
  2. Inside the catch block, x = x + 2; will increase the value of x by 2 i.e. its value will become 3 which is the value that will be returned.
  3. Since finally block is supposed to be executed irrespective of whether an exception is thrown or not, this block will be executed but it won't change the return value. Thus, finally block is useless here. Typically, finally block is used to release resources. Check this to learn more about it.

amazing output for try/catch/finally?

In the JSE7 Language Specification §14.1 a return statement is defined as an abrupt termination. If your finally block completes abruptly (your return) the try block ends for the same reason (as defined in §14.20.2):

§14.1
[...] An abrupt completion always has an associated reason, which is one of the following: [...] A return with no value [...] A return with a given value [...]

§14.20.2
[...] If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and reason R is discarded). [...] (reason R is the result of catch).



Related Topics



Leave a reply



Submit