Recent

Author Topic: Exit not working.  (Read 514 times)

GeneCode

  • New Member
  • *
  • Posts: 19
  • What is the cost of lies?
Exit not working.
« on: October 16, 2019, 03:47:20 am »
Code: Pascal  [Select]
  1. procedure TForm1.OpenBtnClick(Sender: TObject);
  2. begin  
  3.       try
  4.         ...
  5.       finally
  6.            ShowMessage('Unable to Open Port!');
  7.            Exit;
  8.       end;
  9.   ..more code here..
  10. end;
  11.    

My code is as above. I did a Breakpoint at Showmessage, and use F8 to trace executions, so it was executed, but right after that it didnt go to Exit line, instead directly to ..more code here...

Why and how can i exit procedure OpenBtnClick after ShowMessage?
Windows 10
Lazarus 1.8.4

jamie

  • Hero Member
  • *****
  • Posts: 2068
Re: Exit not working.
« Reply #1 on: October 16, 2019, 04:16:27 am »
I believe that is how it works.

If you had a EXIT between the Try.. Finally It will skip remaining code to jump to the FINALLY
section.

 Basically, the FINALLY will always get targeted.

and putting EXIT in the Finally Section just exits that section early.

 I could be wrong, since I really try not to use those things too much.

Number 1 at blue screen app creations!

wp

  • Hero Member
  • *****
  • Posts: 6301
Re: Exit not working.
« Reply #2 on: October 16, 2019, 06:54:45 am »
Your are not using the exception block correctly. Your intention seems to be to display your own error message in case of an exception. You must use the try-except block for this purpose, not try-finally. The latter one always (even without an exception) runs into the "finally" section; since you put an "exit" here you ALWAYS exit the procedure at this point.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 5691
    • wiki
Re: Exit not working.
« Reply #3 on: October 16, 2019, 09:16:57 am »
If you compile with fpc trunk it will give you an error:
project1.lpr(4,3) Error: Control flow statements are not allowed in a finally block