Return value changed after finally
Posted
by
Nestor
on Stack Overflow
See other posts from Stack Overflow
or by Nestor
Published on 2014-06-12T09:20:37Z
Indexed on
2014/06/12
9:24 UTC
Read the original article
Hit count: 243
java
|try-catch-finally
I have the following code:
public bool ProcessData(String data)
{
try
{
result= CheckData(data);
if (TextUtils.isEmpty(result))
{
summary="Data is invalid";
return false;
}
...
finally
{
Period period = new Period(startTime, new LocalDateTime());
String duration = String.format("Duration: %s:%s",
period.getMinutes(), period.getSeconds());
LogCat(duration);
}
return true;
As I learned from this question, the finally block is executed after the return statement. So I modified my code according to that, and in the finally I inserted code that does not modify the output.
Strangely, the code OUTSIDE the finally block does.
My method always returns true.
As suggested, it is not a good idea to have 2 return.
What should I do?
© Stack Overflow or respective owner