使用例外

Ste*_*e88 2 delphi delphi-xe

海兰!我想请求使用例外的建议.我想简化这个......

if (functionSomething1) and (functionSomething2) and (functionSomething3) then
  Do Something Good
else
  Raise Exception(errormsg_functionSomething1IsFalse or errormsg_functionSomething2IsFalse or errormsg_functionSomething3IsFalse);
Run Code Online (Sandbox Code Playgroud)

我的所有函数都返回一个布尔值.

什么是逐个获取错误消息的最佳方法?

例如......如果functionSomething1False,那么我应该得到errormsg_functionSomething1IsFalse

如果functionSomething2False,那么我应该得到 errormsg_functionSomething1IsFalse等...

我认为If-then-else不是一个好的解决方案.

案例或尝试除外?你怎么看?

谢谢你的回答!

Dav*_*nan 7

像这样:

if not functionSomething1 then
  raise EMyException.Create(errormsg_functionSomething1IsFalse);
if not functionSomething2 then
  raise EMyException.Create(errormsg_functionSomething2IsFalse);
if not functionSomething3 then
  raise EMyException.Create(errormsg_functionSomething3IsFalse);

// do something good
Run Code Online (Sandbox Code Playgroud)