Silent Exceptions

From RAD Studio
Jump to: navigation, search

Go Up to VCL Exception Classes

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:

 procedure TForm1.Button1Click(Sender: TObject);
 I, J: Integer;
   for I := 1 to 10 do{ loop ten times }
     for J := 1 to 10 do {loop ten times }
       ListBox1.Items.Add(IntToStr(I) + IntToStr(J));
       if I = 7 then Abort;{ abort after the 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