Инициализация исключительной ситуации
Автор: Xavier Pacheco
{
Copyright © 1999 by Delphi 5 Developer's Guide - Xavier Pacheco and Steve Teixeira
}
unit MainFrm;
interface
uses
SysUtils, Windows, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls, AppEvnts, Buttons;
type
ENotSoBadError = class(Exception);
EBadError = class(Exception);
ERealBadError = class(Exception);
TMainForm = class(TForm)
btnNotSoBad: TButton;
btnBad: TButton;
btnRealBad: TButton;
appevnMain: TApplicationEvents;
procedure btnNotSoBadClick(Sender: TObject);
procedure btnBadClick(Sender: TObject);
procedure btnRealBadClick(Sender: TObject);
procedure appevnMainException(Sender: TObject; E: Exception);
public
end;
var
MainForm: TMainForm;
implementation
{$R *.DFM}
procedure TMainForm.btnNotSoBadClick(Sender: TObject);
begin
raise ENotSoBadError.Create('This isn''t so bad!');
end;
procedure TMainForm.btnBadClick(Sender: TObject);
begin
raise EBadError.Create('This is bad!');
end;
procedure TMainForm.btnRealBadClick(Sender: TObject);
begin
raise ERealBadError.Create('This is real bad!');
end;
procedure TMainForm.appevnMainException(Sender: TObject; E: Exception);
var
rslt: Boolean;
begin
if E is EBadError then
begin
{ Show a custom message box and prompt for application termination. }
rslt := MessageDlg(Format('%s %s %s %s %s', ['An', E.ClassName,
'exception has occurred.', E.Message, 'Quit App?']),
mtError, [mbYes, mbNo], 0) = mrYes;
if rslt then
Application.Terminate;
end
else if E is ERealBadError then
begin // Show a custom message
// and terminate the application.
MessageDlg(Format('%s %s %s %s %s', ['An', E.ClassName,
'exception has occured.', E.Message, 'Quitting Application']),
mtError, [mbOK], 0);
Application.Terminate;
end
else // Perform default exception handling
Application.ShowException(E);
end;
end.
|