异常处理:尝试块中的更多语句会导致可犯错的代码吗?

发布于 2025-01-23 22:39:49 字数 515 浏览 2 评论 0原文

在许多语言中,异常处理使用两个或多个代码块,尝试和一个或多个catch's。

v语言(v0.2.4),尽管不同的语法仅允许中的单个语句尝试< /代码>。开发人员的动机之一是该语言不允许尝试块,因为那是“容易出错”。

我看不到任何支持这一点的论点,所以我的问题是:在尝试块中允许更多的语句导致犯错的代码?

我在尝试中只有一个语句看到了一个很大的缺点:在需要附加完全相同的错误处理程序的情况下,您可能最终会获得更多代码重复。可以通过将代码转移到错误处理功能来缓解这一点,在某些情况下,这是一个不必要的函数声明,您需要多次拨打另一个函数,而且看起来很愚蠢。

从附带说明,只有一个陈述的一个好处是您不需要在外部声明变量,在某些情况下,这可能很有用。

In many languages exception handling uses two or more blocks of code, try and one or more catch's.

Exception handling in V language (v0.2.4), although different syntax only allows a single statement in try. One of the developers motivation is that the language won't allow try blocks because that is "error prone".

I can't see any arguments that supports this so my question is: does allowing more statements in a try block lead to fallible code?

I see a quite big drawback with only one statement in a try: you may end up with more code duplication in cases where you need to attach exact same error handler. This can be alleviated to some degree by moving code to a error handling function, in some cases this is an unnecessary function declaration that you'd need to call multiple times following another and it looks quite stupid.

On a side note, one benefit with only one statement is that you don't need to declare the variable outside, this can be useful in some cases.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文