I'd like to know if it is possible to get the return value of a function inside a finally block.
I have some code that is like this.
try
{
return 1;
}
finally
{
//Get the value 1
}
I know it's possible by adding a variable that can hold the returned value. But I was wondering if it was possible to get the value in any way.
Thanks
Returning from inside a finally block will cause exceptions to be lost. A return statement inside a finally block will cause any exception that might be thrown in the try or catch block to be discarded.
Yes, we can write a return statement of the method in catch and finally block.
When try and finally block both return value, method will ultimately return value returned by finally block irrespective of value returned by try block.
If the return in the try block is reached, it transfers control to the finally block, and the function eventually returns normally (not a throw).
No, you can't do that.
int value = -1;
try
{
value = 1;
}
finally
{
// Now the value is available
}
return value;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With