Discussions about product bugs & problems!
Note: This is no replacement for the Official ETM Support!
4 posts • Page 1 of 1
4 posts
• Page 1 of 1
dbindernagel
Posts:161
Joined: Mon Feb 23, 2015 1:34 pm
Return statement in try block skips finally block
Postby dbindernagel »
Hello,
I found the following behaviour using a try-finally block:
A return statement in the try block skips the execution of the finally block.
Is this intented behaviour in CONTROL? Normally I would expect the finally block to always execute.
To reproduce just create a new panel and add a PUSH_BUTTON with the following Clicked event:
Re: Return statement in try block skips finally block
Postby gschijndel »
It is the behavior I would expect from a return statement, return immediately from the function. Perhaps using the break statement results in the behavior you want.
Many people regard using return, break, continue or goto statements in the middle of a block as bad practice.
Re: Return statement in try block skips finally block
Postby dbindernagel »
I agree in general about the return statment not being called in the "middle" of a block but there are valid use cases, for example I was using the return in guard clauses where this usage is recommended.
Example:
main(mapping event)
{
disableControls();
try
{
if (!userInputAIsValid())
return;
if (!userInputBIsValid())
return;
startProcess();
}
finally
{
enableControls();
}
}
Regardless of this, a Finally block should always be called. This is the behaviour in every other language I know (not saying there are no exceptions but C++, C#, etc. all work like this).
I rewrote this and do not depend on a fix but I still think it is a bug that should be fixed.