-5

Is there a way to abort the execution of a method without raising an EAbort exception ?

Here is a problem:

....

try
  i := strtoint(Edit1.Text);
  if not (i=10) then
  begin
    showmessage('Value for I must be 10 !');
    sysutils.abort;
  end;
except
  showmessage('Wrong Value for i !');
end;

showmessage('Execution continue ...');

...

In this example program execution will not be aborted if value for i is 5 ... it will show message "Value for I must be 10 !", then message "Wrong value for i !" (because EAbort exception is raised) and then message "Execution continue ...". I know how to solve this situation, but I need general solution for sysutils.abort without EAbort exception.

Johan
  • 74,508
  • 24
  • 191
  • 319
OnLineNG
  • 67
  • 6
  • System.Halt; "Halt performs an abnormal termination of a program and returns to the operating system." – Tom Brunberg Aug 24 '16 at 11:16
  • No ... Halt and/or Application.Terminate will terminate program and close it ... but I don't wont to do that ... I need to abort that procedure only, just like sysutils.Abort do ... (If it is not in try/except block)... – OnLineNG Aug 24 '16 at 11:22
  • 7
    You said: "*Is there a way to abort delphi program execution ...*". – Tom Brunberg Aug 24 '16 at 11:24
  • 4
    `Exit` will end a procedure. – LU RD Aug 24 '16 at 11:26
  • Exit will do what I need in some cases. But, if I need to put this code in eg. OnValidate event of TField and replace "sysutils.abort" with "Exit" then I will get "Wrong value" message, but dataset changes will be posted (not aborted as with sysutils.abort). – OnLineNG Aug 24 '16 at 11:37
  • 7
    @OnLineNG, Please stop expanding the question in comments. You're supposed to: 1: think about your question. 2: do some research. 3: formulate a complete query with all the details needed to answer it. This is not an online chat session. And you never mentioned anything about dataset, fields or events in your question. – Johan Aug 24 '16 at 11:46
  • 1
    You want to leave the procedure without raising an exception, and without using `exit`? Is that correct? Are you prepared for the possibility of disappointment. Could you provide motivation for the question? – David Heffernan Aug 24 '16 at 13:13
  • @OnLineNG To prevent the current value of the field from the OnValidate event handler to be stored in the database you need to raise an exception. Well that is what documentation for OnValidate event says. – SilverWarior Aug 24 '16 at 18:10

1 Answers1

10

Just exiting the current method
Use plain exit if exiting a procedure.
Or exit(return_code) if exiting a function (*).

Note that exit will not be caught in a try-except block, but it will be caught in a try-finally block.
If you want more fine grained control you can break loops using break and continue.
Finally there is the option of goto. This is the option of last resort and is generally considered a code smell.

Sample:

function ExitBreakContinue(WhatToDo: integer): string;
var
  a,b,i: integer;
label SomeLabel;
begin
  a:= 0; b:= 0;
  try
    for i:= 0 to 10 do begin
      a:= a + 1;
      case WhatToDo of
        1: exit('exit_called');
        2: continue;
        3: break;
        4: abort;
        5: goto SomeLabel;
      end;
      b:= b + 1;
    end; {for i}
    Result:= 'a,b = '+IntToStr(a)+','+IntToStr(b);
  finally
    Result:= Result + ' finally';
  end;
  SomeLabel: Result:= Result + ' goto '; 
end;

Closing your application
In a graphical application you'd call application.terminate.
In a console application call halt with an exit code, or RunError with an error code.

halt/RunError will also work in a graphical application, but is not recommended because it does not allow the application to close down gracefully.
This will upset your users because the OnCloseQuery event of the mainform cannot be triggered.
Normally you'd (offer to) save the user's work in this event handler.

(*) exit(return_code) is supported in Delphi 2009 and newer.

Community
  • 1
  • 1
Johan
  • 74,508
  • 24
  • 191
  • 319
  • No ... Halt and/or Application.Terminate will terminate program and close it ... but I don't wont to do that ... I need to abort that procedure only, just like sysutils.Abort do ... (If it is not in try/except block)... – OnLineNG Aug 24 '16 at 11:22
  • If I replace "sysutils.abort" with ExitBreakContinue(4) I will get same behaviour as in my example ... – OnLineNG Aug 24 '16 at 11:42
  • Exit will do what I need in some cases. But, if I need to put this code in eg. OnValidate event of TField and replace "sysutils.abort" with "Exit" then I will get "Wrong value" message, but dataset changes will be posted (not aborted as with sysutils.abort). – OnLineNG Aug 24 '16 at 11:42
  • 4
    @OnLineNG Clearly you are deeply confused. You don't understand your specific problem, but sadly you did not ask a question about your specific problem. You asked a clear question about something else, and here is your answer. That you asked the wrong question is your problem. You need to stop looking for magic bullets and spend a bit more time working out what your problem really is. – David Heffernan Aug 24 '16 at 14:07