这已经被报告为RSP-25603: "Exception.RaiseOuterException can cause wrong W1035 warning"。
鉴于以下(演示)函数F
,我已将异常引发语句更改为现在链异常:
--- before
+++ after
@@ -1,11 +1,11 @@
function F(X: NativeInt): NativeInt;
begin
try
Result := 1 div X;
except
on EDivByZero do
- {ECustom}Exception.Create('...');
+ Exception.RaiseOuterException({ECustom}Exception.Create('...'));
else
raise;
end;
end;
Run Code Online (Sandbox Code Playgroud)
现在,Ctrl-F9
给出警告W1035:
[dcc32 警告]:W1035 函数“F”的返回值可能未定义
但是,所有情况都会处理。编译器无法将其识别Exception.RaiseOuterException
为raise
操作。
不幸的FAcquireInnerException: Boolean
是,Exception
该类是私有的,甚至不能True
在派生的自定义类中设置,我可以继续直接提高 ( raise ECustomException.Create
)。
有什么方法可以让编译器理解,同时保持异常链接?不然我能想到{$Warn No_RetVal Off}
。我还能如何解决这个警告?
我能想到的避免警告而不禁用警告的一种方法是执行以下操作:
function F(X: NativeInt): NativeInt;
begin
try
Result := 1 div X;
except
on E: Exception do
begin
if E is EDivByZero then
Exception.RaiseOuterException({ECustom}Exception.Create('...'));
raise;
end;
end;
end;
Run Code Online (Sandbox Code Playgroud)
更新:另一种方法,如评论中所述,是简单地定义一个在运行时实际未达到的返回值,例如:
function F(X: NativeInt): NativeInt;
begin
try
Result := 1 div X;
except
on E: EDivByZero do
begin
Exception.RaiseOuterException({ECustom}Exception.Create('...'));
Result := 0; // <-- just to keep the compiler happy
end;
end;
end;
Run Code Online (Sandbox Code Playgroud)