Delphi重新提升异常(作为参数传递给过程)

Delphi重新提升异常(作为参数传递给过程),第1张

概述这是重新引发异常并且运行良好的示例 try raise Exception.Create('Exception msg'); except on E: Exception do begin if not(e is EConvertError) then raise; // re-raise exception end; end; 这是重新引发异常并且运行良好的示例

try    raise Exception.Create('Exception msg');  except    on E: Exception do    begin      if not(e is EConvertError) then        raise; // re-raise exception    end;  end;

这是我的Custemize方法

//    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.”消息之后

如何引发与基本异常相同的异常类型

解决方案是引发TObject(AcquireExceptionObject); //< - 我想解决“E:

type  ECustomizedException = class(Exception); uses   fib,SysUtils,Systemclass 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','</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.
解决方法 您遇到的问题是,如果在except块中捕获到异常,则“end”将释放您刚刚引发的异常实例.所以下一个except块将捕获已经发布的Exception实例.但是你可以通过调用AcquireExceptionObject来防止这种情况,这会使你成为异常实例的所有者.

因为你“不能”使用加注; (System.@ RaiseAgain)你可以使用raise AcquireExceptionObject抛出相同的异常实例;

总结

以上是内存溢出为你收集整理的Delphi重新提升异常(作为参数传递给过程)全部内容,希望文章能够帮你解决Delphi重新提升异常(作为参数传递给过程)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1237471.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存