Raise an exception
1   Raise
2   Raise Object reference
3   Raise Object reference At Address pointer
The Raise keyword creates an exception object that is passed to the Delphi exception handler.
You would only raise an exception in literally exceptional circumstances. This is partly because of the resource and performance overheads incurred, but also because there are neater ways for application error handling, such as return codes from functions.
Version 1
On its own, Raise is used inside the Except clause of a Try statement. It simply re-raises the current exception for handling at a higher level in the application.
Version 2
Uses an new exception object to report an exception. Normally, you would use an Exception object, or an inherited Exception object, but you are not restricted to do so. The exception address is that of the raise statement.
You can create the object at the time of the raise:
Raise Exception.Create('Error happened');
Version 3 As for version 2, but overriding the address value of the exception.
In all cases, when the Raise call is made, code execution jumps to the Delphi exception handler - it either terminates the program, or uses the current Try statement to handle it.
Warning use only when appropriate.
Related commands
Except Starts the error trapping clause of a Try statement
Finally Starts the unconditional code section of a Try statement
On Defines exception handling in a Try Except clause
Try Starts code that has error trapping
 Author links

 Buy Website Traffic at

 Buy Proxies at
 Download this web site as a Windows program.

Example code : Use of Raise in a function
  fred, jim : string;
  // Set up some sample names
  fred := 'Good name';
  jim  := 'Badname  ';

  // Try to swap these names
    ShowMessage(fred+' swapped = '+SwapNames(fred));
    ShowMessage(jim+' swapped = '+SwapNames(jim));
    On E : Exception do

// Swaps first and second names in the passed name string
// Raises an exception if the name is invalid
function TForm1.SwapNames(name: string): string;
  blankPos : Integer;
  i        : Integer;
  nameLen  : Integer;
  // Clear the result string to indicate no success yet
  Result := '';

  // Find the position of the last name
  blankPos := Pos(' ', name);

  // If found, and position is short of the name end
  // then we are OK so far
  nameLen := Length(name);
  if (blankPos > 0) and (blankPos < nameLen)
    // Find the start of the second name
    i := blankPos + 1;
      // If last name start found, swap first and last names
      if name[i] <> ' '
      then Result := Copy(name, i, nameLen-i+1) + ' ' +
                     Copy(name, 1, blankPos-1)

      else Inc(i);
    until (i > nameLen) or (Length(Result) > 0);

  // Couldn't swap first and second names ?
  if Length(Result) = 0
  then Raise Exception.CreateFmt('Invalid name : ''%s''', [name]);
Show full unit code
   Good name swapped = name Good
   Invalid name : 'Badname '
Example code : Raise a new exception type
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
unit Unit1;
  Forms, Dialogs;
  EBadAge = class(Exception);
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
Form1: TForm1;
{$R *.dfm} // Include form definitions
procedure TForm1.FormCreate(Sender: TObject);

  age : Integer;
  exc : EBadAge;
  // Simple code that raises a new exception type
  age := 23;
  if age < 30
    // Create a new EBadAge exception object
    exc := EBadAge.CreateFmt('Bad age : %d',[age]);

    // Now raise this new exception object
    Raise exc;
Hide full unit code
   A Delphi error dialog is shown with message :
   Bad age : 23
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Home Page