SmartPascal
Raise
Keyword
Raise an exception
1   Raise
2   Raise Object reference
3   Raise Object reference At Address pointer
Description
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.
Notes
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
 
Example code : Use of Raise in a function
var
  fred, jim : string;
begin
  // Set up some sample names
  fred := 'Good name';
  jim  := 'Badname  ';

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

// Swaps first and second names in the passed name string
// Raises an exception if the name is invalid
function TForm1.SwapNames(name: string): string;
var
  blankPos : Integer;
  i        : Integer;
  nameLen  : Integer;
begin
  // 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)
  then
  begin
    // Find the start of the second name
    i := blankPos + 1;
    repeat
      // 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);
  end;

  // Couldn't swap first and second names ?
  if Length(Result) = 0
  then Raise Exception.CreateFmt('Invalid name : ''%s''', [name]);
end;
Show full unit code
   Good name swapped = name Good
   Invalid name : 'Badname '
 
Example code : Raise a new exception type
var
  age : Integer;
  exc : EBadAge;
begin
  // Simple code that raises a new exception type
  age := 23;
  if age < 30
  then
  begin
    // Create a new EBadAge exception object
    exc := EBadAge.CreateFmt('Bad age : %d',[age]);

    // Now raise this new exception object
    Raise exc;
  end;
end;
Show full unit code
   A Delphi error dialog is shown with message :
  
   Bad age : 23