RAD Studio |  |

Silent Exceptions
See Also
Collapse All

VCL applications handle most exceptions that your code doesn't specifically handle by displaying a message box that shows the message string from the exception object. You can also define "silent" exceptions that do not, by default, cause the application to show the error message.

Silent exceptions are useful when you don't intend to report an exception to the user, but you want to abort an operation. Aborting an operation is similar to using the Break or Exit procedures to break out of a block, but can break out of several nested levels of blocks.

Silent exceptions all descend from the standard exception type EAbort. The default exception handler for VCL applications displays the error-message dialog box for all exceptions that reach it except those descended from EAbort.

Note: For console applications, an error-message dialog is displayed on any unhandled EAbort exceptions.
There is a shortcut for raising silent exceptions. Instead of manually constructing the object, you can call the Abort procedure. Abort automatically raises an EAbort exception, which breaks out of the current operation without displaying an error message.
Note: There is a distinction between Abort and abort. abort kills the application.
The following code shows a simple example of aborting an operation. On a form containing an empty list box and a button, attach the following code to the button's OnClick event:
Copy Code

    procedure TForm1.Button1Click(Sender: TObject);
    var
    I, J: Integer;
    begin
      for I := 1 to 10 do{ loop ten times }
        for J := 1 to 10 do {loop ten times }
        begin
          ListBox1.Items.Add(IntToStr(I) + IntToStr(J));
          if I = 7 then Abort;{ abort after the 7th iteration of outer loop}
        end;
    end;

Copy Code

    void __fastcall TForm1::Button1Click(TObject* Sender)
    {
      for (int i = 1; i <= 10; i++) // loop ten times
        for (int j = 1; j <= 10; j++) // loop ten times
        {
          ListBox1->Items->Add(IntToStr(i) + IntToStr(j));
          if (i == 7)
            Abort(); // abort after 7th iteration of outer loop
        }
    }

Note that in this example, Abort causes the flow of execution to break out of both the inner and outer loops, not just the inner loop.

See Also

VCL Exception Classes

Default Exception Handling in VCL

Defining Your Own VCL Exceptions

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