这是重新引发异常并且运行良好的示例
try
raise Exception.Create('Exception msg');
except
on E: Exception do
begin
if not(e is EConvertError) then
raise; // re-raise exception
end;
end;
这是我的自定义方法
// uses fib //fibplus EFIBInterBaseError
procedure CustomizeException(e: Exception);
var
s: String;
begin
if E is EFIBInterBaseError then
begin
if Pos('unique',e.Message)>0 then
begin
s := 'record';
if Pos('CUSTOMMERS_IDX1',e.Message)>0 then
s:= 'Custommer Number';
raise TCustomizedException.CreateFmt('The %s is already exists.',[s]);
end
else
if Pos('</CMSG>',e.Message)>0 then
Begin
raise TCustomizedException.CreateFmt('%s',
[StrUtilsEx.GiveInTags(e.Message,'<CMSG>','</CMSG>')]
);
End
else
raise EFIBInterBaseError.CreateFmt('%s',[e.Message]);
end
else
raise Exception.Create(e.Message); //e;// e.Create(e.Message);
end;
但
try
raise EConvertError.Create('Error Message');
except on e : exception do
Begin
ShowMessage(Format('%s -> %s',[e.ClassName , e.Message])); //(1)
try
CustomizeException(e);
except on e2: Exception do
ShowMessage(Format('%s -> %s',[e2.ClassName , e2.Message])); //(2)
end;
End;
end;
结果
(1)->EConvertError -> 错误信息
(2)->异常-> 错误信息
当我像这个代码一样更改最后一行时运行良好
else
raise e;
(1)->EConvertError -> 错误信息
(2)-> EConvertError -> 错误信息
但我收到“模块'Test.exe'中地址00405F04的访问冲突。读取地址00000000。” 留言后
如何引发与基本异常相同的异常类型
解决方案是raise TObject(AcquireExceptionObject); //<- 我想用“E:
type
ECustomizedException = class(Exception);
uses
fib,SysUtils,System
class procedure SystemEx.CustomizeException(E : Exception);
var
s: String;
begin
if E is EFIBInterBaseError then
begin
if Pos('unique',e.Message)>0 then
begin
s := 'Record';
if Pos('CUSTOMMER_IDX1',e.Message)>0 then
s:= 'Custommer';
raise ECustomizedException.CreateFmt('%s is already exists.',[s]);
end
else
if Pos('</CMSG>',e.Message)>0 then
Begin
raise ECustomizedException.CreateFmt('%s',
[StrUtilsEx.GiveInTags(e.Message,'<CMSG>','</CMSG>')]
);
End
else
raise EFIBInterBaseError.CreateFmt('%s',[e.Message]);
end
else
raise TObject(AcquireExceptionObject); //<- I would like to solve with "E : Exception" param
// raise Exception.Create(e.Message); //e;// e.Create(e.Message);// Exception.Create(E.Message);
End.