异常处理:尝试块中的更多语句会导致可犯错的代码吗?
在许多语言中,异常处理使用两个或多个代码块,尝试
和一个或多个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论