Recent

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

GeneCode

  • New Member
  • *
  • Posts: 25
  • 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: 6090
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.

The only true wisdom is knowing you know nothing

wp

  • Hero Member
  • *****
  • Posts: 11854
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.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9791
  • Debugger - SynEdit - and more
    • 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


 

TinyPortal © 2005-2018