RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
System.Exit Function

Exits from the current procedure.

Pascal
procedure Exit;
C++
Exit();

In Delphi, the Exit procedure immediately passes control away from the current procedure. If the current procedure is the main program, Exit causes the program to terminate. 

Exit will cause the calling procedure to continue with the statement after the point at which the procedure was called.

Note: Exit passes control away from the current procedure, not merely the current block. But Exit does not violate the flow of control dictated by a try..finally construct; if Exit is called inside the try clause, the finally clause is still executed.
Beginning in Delphi 2009, Exit can take a parameter specifying a result. The parameter must be of the same type as the result of the function. For example:

               function DoSomething(aInteger: integer): string
               begin
               if aInteger < 0 then
                  begin
                     Exit(‘Negative’);
                  end;
                  Result := ‘Positive’;
               end;

 

Delphi Examples: 

{
This example uses a memo and six buttons on a form. This
example illustrates the different ways to exit a function.
}
function GetAString(input: string): string;
var
  mystring : string;
begin
  mystring := DateToStr(Date);
  if (input = 'date') then
    Exit(mystring);
  if (input = 'Tuesday') then
  begin
    Result := 'Tuesday';
    Exit;
  end;
  Result := 'nothing';
end;

function GetANumber(input: string): Integer;
var
  myInt : Integer;
begin
  myInt := Round(Date);
  if (input = 'date') then
    Exit(myInt);
  if (input = 'Tuesday') then
  begin
    Result := 2;
    Exit;
  end;
  Result := 0;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Lines.Add(GetAString('date'));
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Memo1.Lines.Add(GetAString('babble'));
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  Memo1.Lines.Add(GetAString('Tuesday'));
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  Memo1.Lines.Add(IntToStr(GetANumber('date')));
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
  Memo1.Lines.Add(IntToStr(GetANumber('nothing')));
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
  Memo1.Lines.Add(IntToStr(GetANumber('Tuesday')));
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!