相关资料:
http://blog.csdn.net/a20071426/article/details/10160171
https://www.cnblogs.com/jijm123/p/11010309.html
实例代码:
unit Unit1; interface uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls; type
TMyException = class(Exception)
FMessage: string;
FInt: Integer;
FStr: string;
FBool: Boolean;
public
constructor Create(const Msg: string);
property Message: string read FMessage write FMessage;
property Int: Integer read FInt;
property Str: string read FStr;
property Bool: Boolean read FBool;
end; TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject);
var
Int1: Integer;
begin
try
Int1 := StrToInt('A');
except
raise Exception.CreateFmt('%d = %s',[, '您的程序在Unit1单元35行出错了!']);//1001=您的程序在Unit1单元35行出错了!
end;
end; { TMyException } constructor TMyException.Create(const Msg: string);
begin
FMessage := Msg;
FInt := ;
FStr := 'NO';
FBool := False;
end; procedure TForm1.Button2Click(Sender: TObject);
begin
try
raise TMyException.Create('aaaaa');
except
on e:TMyException do
begin
ShowMessage(e.Message); //aaaaa
ShowMessage(IntToStr(e.Int)); //
ShowMessage(e.Str); //NO
ShowMessage(BoolToStr(e.Bool)); //
end;
end;
end; procedure TForm1.Button3Click(Sender: TObject);
begin
raise Exception.Create('抛出异常'); //抛出异常
end; procedure TForm1.Button4Click(Sender: TObject);
var
exc: Exception;
begin
exc := Exception.Create('发现异常'); //发现异常
raise exc;
end; end.