登录后如何重新引发Delphi异常? [英] How should I re-raise a Delphi exception after logging it?

查看:142
本文介绍了登录后如何重新引发Delphi异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你知道一种在Delphi代码中捕获,记录和重新引发异常的方法吗?
一个简单的例子:

 程序TForm3.Button1Click(Sender:TObject); 
begin
try
raise Exception.Create('Bum');
除了
对于E:异常do
begin
MyHandleException(E);
结束
结束
结束

程序TForm3.MyHandleException(AException:Exception);
begin
ShowMessage(AException.Message);
LogThis(AException.Message);
// raise AException; - 这将访问违反
结束;

所以我需要在除了块中重新提升它,但我想知道是否有更好的方式来编写自己的方法来处理和(在特定条件下)来重新引发异常。

解决方案

- 只在特定条件下写出异常,写入

  procedure TForm3.Button1Click(Sender:TObject); 
begin
try
raise Exception.Create('Bum');
除了
在E:异常do
开始
如果MyHandleException(E)然后
加注;
结束
结束
结束

函数TForm3.MyHandleException(AException:Exception):boolean;
begin
ShowMessage(AException.Message);
result:= true / false;
结束


Do you know a way to trap, log, and re-raise exception in Delphi code? A simple example:

procedure TForm3.Button1Click(Sender: TObject);
begin
  try
    raise Exception.Create('Bum');
  except
    on E: Exception do
    begin
      MyHandleException(E);
    end;
  end;
end;

procedure TForm3.MyHandleException(AException: Exception);
begin
  ShowMessage(AException.Message);
  LogThis(AException.Message);  
  // raise AException; - this will access violate
end;

So I need to re-raise it in the except block but I was wondering if there is a better way to write my own method to handle and (on specific conditions) to re-raise exceptions.

解决方案

If you want to re-raise the exception only under certain conditions, write

procedure TForm3.Button1Click(Sender: TObject);
begin
  try
    raise Exception.Create('Bum');
  except
    on E: Exception do
    begin
      if MyHandleException(E) then
        raise;
    end;
  end;
end;

function TForm3.MyHandleException(AException: Exception): boolean;
begin
  ShowMessage(AException.Message);
  result := true/false;
end;

这篇关于登录后如何重新引发Delphi异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆