Perl 中处理异常的最佳方法是什么?
我注意到 Exception.pm 和 Error.pm 似乎并没有在 Perl 社区中广泛使用。这是因为异常处理的 eval
占用空间很大吗?
另外,Perl 程序对于异常处理一般来说似乎有更宽松的政策。这有令人信服的理由吗?
无论如何,Perl 中异常处理的最佳方法是什么?
I've noticed that Exception.pm and Error.pm don't seem to be extensively used in the Perl community. Is that due to the large footprint of eval
for exception handling?
Also Perl programs appear to have a much more lenient policy regarding exception handling in general. Is there a compelling reason for this?
In any event what would be the best method for exception handling in Perl?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Perl 社区的共识似乎是 Try::Tiny 是首选方式异常处理。您提到的“宽松政策”可能是由于以下原因的组合:
你无法避免处理异常。)
异常机制。)
不需要异常处理的报告生成。)
请注意,最后一项意味着您将看到很多这样的代码:
这是异常处理,即使它不使用 try/catch 语法。但它很脆弱,并且会在许多大多数人没有想到的微妙边缘情况下崩溃。 Try::Tiny 和 CPAN 上的其他异常处理模块的编写是为了更容易正确处理。
<子>1。 C 确实有
setjmp()
和longjmp()
,它们可用于非常粗略的异常处理形式。The consensus of the Perl community seems to be that Try::Tiny is the preferred way of doing exception handling. The "lenient policy" you refer to is probably due to a combination of:
you can't avoid dealing with exceptions.)
exception mechanisms.)
report generation where exception handling isn't needed.)
Note that the last item means that you'll see a lot of code like this:
That's exception handling even though it doesn't use try/catch syntax. It's fragile, though, and will break in a number of subtle edge cases that most people don't think about. Try::Tiny and the other exception handling modules on CPAN were written to make it easier to get right.
1. C does have
setjmp()
andlongjmp()
, which can be used for a very crude form of exception handling.切勿按原样测试 $@,因为它是一个全局变量,因此即使测试本身也可以更改它。
通用评估模板:
在实践中这是最简单的方法。它仍然为有趣的 $@ 行为留下了很小的空间,但没有什么真正让我担心的。
Never test $@ as is, because it is a global variable, so even the test itself can change it.
General eval-template:
In practice that is the lightest way. It still leaves a tiny room for funny $@ behaviour, but nothing that really concerned me enough.
正如已经提到的,您可以使用传统方式 eval,但如果您愿意要使用更复杂的异常捕获(包括异常对象),那么我建议使用 try-catch-finally 块。
有相当多的 Perl 模块提供了它,例如 Nice::Try 和 Syntax::Keyword::Try,但是 Syntax::Keyword::Try 不提供异常变量赋值或异常类捕获,如
完全披露:我是开发人员Nice::Try
As it has been mentioned you can use the traditional way with eval, but if you want to use more elaborate exception trapping, including with exception objects, then I recommend using the try-catch-finally blocks.
There are quite a few perl modules that provide it such as Nice::Try and Syntax::Keyword::Try, but Syntax::Keyword::Try does not provide exception variable assignment or exception class catch like
Full disclosure: I am the developer of Nice::Try