德尔福在整个代码中使用try / except。邪恶? [英] Delphi. Using try/except throughout the code. Evil?

查看:93
本文介绍了德尔福在整个代码中使用try / except。邪恶?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个理论上的问题。

This is rather theoretical question.

我在最近的项目中遇到了这种做法-几乎每个过程和函数都包裹在try / except子句中,如下所示:

I've encountered this practice in a recent project - almost every procedure and function is wrapped into try/except clause like so:

function TMyClass.DoFoo(aBar: Integer): Boolean;
begin
  try
    .. do something .. // Proper checks for common errors inside
    Result := True;
  except
    LogException(ClassName + '.DoFoo'); // Write exception to log
    Result := False; // Indicate function did not performed what it was asked for
    // Exit without re-raising exception
  end;
end;

procedure TMyClass.Buzz(Sender: TObject);
begin
  try
    .. do something .. // Proper checks for common errors inside
  except
    LogException(ClassName + '.DoFoo'); // Write exception to log
    // Exit without re-raising exception
  end;
end;

这看起来像很多重复。但是我可以看到其背后的某种逻辑。如果将每个过程和函数包装到 try..except 中,您将知道异常的大概位置,并让程序继续运行而不会在用户身上弹出崩溃消息,但是将它们写出来

This looks like a lot of repetition. However I can see some kind of logic behind it. If you wrap every procedure and function into try..except you would know approximate location of the exception and let the program continue working without popping crash messages at the user, but write them to a log for future analysis.

但是,这种做法被认为是邪恶的。

However this practice is considered to be evil. Why exactly is that?

编辑:


  • 这是关于项目的问题我将与之合作,不是我自己发明的

  • 我对madExcept非常了解,我在其他项目中使用了它

  • 该项目中的功能和过程照常布置,没有迹象表明这些过程被编写为仅用于替换异常流

  • This is a question about project I'm going to work with, I did not invented it myself
  • I'm well aware about madExcept and I use it in other projects
  • Functions and procedures in that project are layed out as usual, there are no signs of procedures being written as functions for sole purpose of replacing exceptions flow

推荐答案

有很多种种原因使之成为一个可怕的想法。

There are all sorts of reasons why this is a simply dreadful idea.

异常处理现在正在驱动所有功能的设计。他们现在都需要返回布尔值。任何实际的返回值都必须传递出参数。这将破坏代码的可读性和函数的可组合性。

The exception handling is now driving the design of all your functions. They all now need to return booleans. Any real return values have to pass through out parameters. That will destroy the readability of your code and composability of your functions.

现在,您需要检查所进行的每个函数调用的返回值,并将该异常向上传播。因此,您已经放弃了异常处理的所有好处。关于例外的伟大之处在于,您可以在很大程度上将正常流量与异常流量分开。有了您建议的更改,前端和中间的流量就会异常增加。

And you now need to check the return value of every function call you make and propagate that exception upwards. You've therefore abandoned all the benefits of exception handling. The great thing about exceptions is that you can largely separate normal flow from exceptional flow. With your proposed change, exceptional flow in front and centre.

另一个大问题是,您迫使代码尽早处理错误。但是,引发异常的代码可能无法处理错误。如果让错误向上浮动,则代码可以在能够执行此操作的地方处理错误。

The other big problem is that you force the code to handle the error as early as possible. But the code at the point where the exception is raised might not be in a position to handle the error. If you let the error float upwards then the code can handle the error at the point where it is able to do so.

如果您希望程序记录错误的详细信息,并且不向用户显示消息,则使用异常处理很容易实现。您可以使用madExcept,EurekaLog或JclDebug之类的工具来帮助完善日志记录。

If you want the program to log details of an error, and not show a message to the user, that is easy to achieve with exception handling. You can use tools like madExcept, EurekaLog or JclDebug to help make the logging comprehensive.

这篇关于德尔福在整个代码中使用try / except。邪恶?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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