如何处理程序语言中的异常? [英] How do I handle exceptions in a procedural language?

查看:113
本文介绍了如何处理程序语言中的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用C或Perl等过程语言进行异常处理? (我知道Perl也做OO)在Perl的程序代码中处理异常的最好方法是什么?

How do I do exception handling in a procedural language like C or Perl? (I know Perl also does OO.) What’s the best way to handle exception in procedural code in Perl?

推荐答案

在Perl 5使用 eval die 完成异常处理。你只需评估代码的正文,如果它死了,你可以检查 $ @ 的错误。如果你想要正确地做到这一点并不容易,这就是为什么各种try / catch模块存在的。您可能对 Try :: Tiny 感兴趣,它没有依赖关系,并且描述了所有在使用naive eval异常处理时,必须处理这些问题。 (另请参阅Try :: Tiny的作者此博文)。

In Perl 5 the exception handling is done using eval and die. You simply eval the body of code and if it dies, you can inspect $@ for the error. It’s not exactly this easy if you want to do it properly, which is why the various try/catch modules exist. You might be interested in Try::Tiny which has no dependencies and describes all the gotchas you have to deal with when using the naive eval exception handling. (Also see this blog post by Try::Tiny’s author.)

这篇关于如何处理程序语言中的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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