Return statement in try block skips finally block

Discussions about product bugs & problems!
Note: This is no replacement for the Official ETM Support!
Search

Post Reply
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

Post by 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:

Code: Select all

main(mapping event)
{
  try
  {
    PUSH_BUTTON1.enabled = false;
    return;
  }
  finally
  {
    PUSH_BUTTON1.enabled = true;
  }
}
WinCC OA 3.18 P012

User avatar
amichon
Posts: 93
Joined: Sat May 17, 2014 3:49 pm

Re: Return statement in try block skips finally block

Post by amichon »

I reproduce the same issue.
WINCC OA 3.18 P012

gschijndel
Posts: 373
Joined: Tue Jan 15, 2019 3:12 pm

Re: Return statement in try block skips finally block

Post by 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.

dbindernagel
Posts: 161
Joined: Mon Feb 23, 2015 1:34 pm

Re: Return statement in try block skips finally block

Post by 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:

Code: Select all

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.

Post Reply
4 posts • Page 1 of 1