gpt4 book ai didi

delphi - 在delphi 7中, `try ... except raise; end;`有意义吗?

转载 作者:行者123 更新时间:2023-12-03 14:49:35 28 4
gpt4 key购买 nike

在我维护的一些 Delphi 7 代码中,我注意到很多以下情况的实例:

with ADOQuery1 do begin
// .. fill out sql.text, etc
try
execSQL;
except
raise;
end;
end;

在我看来,这些 try block 可以被删除,因为它们什么也不做。然而,我对可能出现的微妙副作用持谨慎态度。

有人能想到这些 block 实际上可以做任何没有它们就不会发生的事情的实例吗?

最佳答案

在这种情况下,raise 操作没有任何效果,应该被删除,因为它只是重新引发异常 block 刚刚捕获的异常。 raise 通常用于在没有适当的错误处理可用时将控制转移到 block 的末尾。下面我们处理自定义异常,但任何其他异常都应该在其他地方处理。

try
someOperation;
except
on e: ECustomException do
SomeCustomHandelr;
else
begin
// the raise is only useful to rethrow the exception to an encompasing
// handler. In this case after I have called my logger code. as Rob
// mentioned this can be omitted if you arent handling anything because
// the compiler will simply jump you to the next block if there is no
// else.
LogUnexpectedException('some operation failed',e);
raise;
end;
end;

请注意,有类似的外观形式,但没有“引发”,这确实会产生吃掉/隐藏任何异常的副作用。非常肆无忌惮的开发者的做法,他们希望能够在竞争对手中占据一席之地。

with ADOQuery1 do begin  
// .. fill out sql.text, etc
try
execSQL;
except
// no handler so this just eats any "errors"
end;

关于delphi - 在delphi 7中, `try ... except raise; end;`有意义吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/805109/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com