在php4中实现try catch的最佳方法
php4 中最接近 try-catch 块的是什么?
我在 xmlrpc 请求期间处于回调中间,无论如何都需要返回一个特定结构的数组。
我必须错误检查对外部资源的所有访问,导致嵌套 if-else 块的深层堆栈,丑陋。
What is the closest you can get to a try-catch block in php4?
I'm in the middle of a callback during an xmlrpc request and it's required to return a specifically structured array no matter what.
I have to error check all accesses to external resources, resulting in a deep stack of nested if-else blocks, ugly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
迟到的答复,我意识到,抱歉。 我希望这仍然与您相关:
首先,我回应您在回复您的帖子时收到的评论。 PHP5 是必经之路。
然而:
如果您可以保证程序在没有返回结构化数组的情况下不可能继续,并且您绝对必须使用 PHP4,那么 exit() 或 die()包含详细的错误信息将为您带来与致命异常相同的效果。
当然,这与优雅相去甚远。 不幸的是,如果你想要一些可捕捉的东西,那么返回值和 if 检查结果是你最好的选择。 有一些标准方法可以传回特定的错误对象,但它仍然是同一件事 - 返回错误对象,如果检查结果是否是错误对象,则进行反应。
不过,请查看 PEAR 的错误对象。
Late answer, I realise, sorry. I hope this is still relevant for you:
First, I'm echoing the comments your got in response to your post. PHP5 is the way to go.
However:
If you can vouch for that the program cannot possibly continue without getting a structured array back, and you absolutely have to work with PHP4, then an exit() or die() with detailed error information will get you much the same effect as a fatal exception would.
That's far removed from being graceful, of course. If you want something catchable, then return values and if-checking the result are your best bet, unfortunately. There are some standard ways of passing back specific error objects, but it's still the same thing - return the error object, if-check whether the result was an error object, react.
Still, take a look at PEAR's error object.