异常与错误处理 [英] Exception vs error handling

查看:63
本文介绍了异常与错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自.Net世界,我习惯了尝试......捕捉......最后

处理错误的方法。 PHP 5现在支持这种方法。但是

我不清楚未处理的错误/异常会发生什么?我是否需要
定义一个默认的错误处理程序以及我的try / catch?我可以将

错误处理与异常混合使用吗?一种方法比

更好吗?


TIA

John

Coming from the .Net world, I am used to the try...catch...finally
approach to error handling. And PHP 5 now supports this approach. But
I am not clear what happens to unhandled errors/exceptioins? Do I
define a default error handler as well as do my try/catch? Can I mix
error handling with the exceptions? Is one approach better than the
other?

TIA
John

推荐答案



" john6630" < jo ****** @ hotmail.comwrote in message

news:1b ************************ ********** @ r15g2000 prd.googlegroups.com ...

"john6630" <jo******@hotmail.comwrote in message
news:1b**********************************@r15g2000 prd.googlegroups.com...

来自.Net世界,我习惯了。 ..catch ...最后

处理错误的方法。 PHP 5现在支持这种方法。但是

我不清楚未处理的错误/异常会发生什么?我是否需要
定义一个默认的错误处理程序以及我的try / catch?我可以将

错误处理与异常混合使用吗?一种方法比

更好吗?
Coming from the .Net world, I am used to the try...catch...finally
approach to error handling. And PHP 5 now supports this approach. But
I am not clear what happens to unhandled errors/exceptioins? Do I
define a default error handler as well as do my try/catch? Can I mix
error handling with the exceptions? Is one approach better than the
other?



try / catch不是.NET exlusive。此外,你来自VB.NET

背景......因为try / catch是MOST *其他*语言的一部分
$。b $ b支持.net,即c,c ++,c#,perl等等自

..NET出现之前。


无论如何,当你不处理时会发生什么任何其他

语言的例外情况?你得到一个不可恢复的崩溃......或者你的语言的错误处理程序

尽可能地处理它。 php也不例外。


你应该计划和设计你想要处理错误的方式。如果你想

美化默认错误处理程序的输出,一定要绑定它。

和其他方法一样... *总是*尝试和处理任何错误你可能会遇到
。如果你不这样做,你会发现自己潜在的内存泄漏

,你的错误会以丑陋的方式向你的顾客展示

jibberish-for-a - 问题的描述。


但是,那只是我。


欢呼

try/catch is not .NET exlusive. further, you are coming from a VB.NET
background...as try/catch has been a part of MOST *other* languages
supported by .net, i.e. c, c++, c#, perl, etc. since before the advent of
..NET.

anyway, what happens when you don''t handle an exception in any other
language? you get an unrecoverable crash...or your language''s error handler
handles it as best it can. php is no different.

you should plan and design the way you want errors handled. if you want to
beautify the output of the default error handler, by all means, tie into it.
as for other methods...*always* try and handle any error you might
encounter. if you don''t, you will find yourself with a potential memory leak
and your errors will show to your customers in an ugly fashion with
jibberish-for-a-description of the problem.

but, that''s just me.

cheers


8月19日晚上7:06,john6630< john6 ... @ hotmail.comwrote:
On Aug 19, 7:06 pm, john6630 <john6...@hotmail.comwrote:

来自.Net世界,我我习惯了尝试...抓住......最后

处理错误的方法。 PHP 5现在支持这种方法。但是

我不清楚未处理的错误/异常会发生什么?我是否需要
定义一个默认的错误处理程序以及我的try / catch?我可以将

错误处理与异常混合使用吗?一种方法比

其他方法更好吗?


TIA

John
Coming from the .Net world, I am used to the try...catch...finally
approach to error handling. And PHP 5 now supports this approach. But
I am not clear what happens to unhandled errors/exceptioins? Do I
define a default error handler as well as do my try/catch? Can I mix
error handling with the exceptions? Is one approach better than the
other?

TIA
John



PHP不支持终于。


但是在其最常部署的配置中,它确实要转储

警告/输出中间的错误信息,因为Dale提到

to。


我的建议:


尽可能地将error_reporting调高:E_ALL | E_STRICT。


使用类似

之类的内容将所有PHP警告/错误转换为异常:


set_error_handler( create_function(''

PHP doesn''t support "finally."

But in its most commonly deployed configuration, it does like to dump
warning/error messages in the middle of your output, as Dale alluded
to.

My advice:

Turn up error_reporting as far as it can go: E_ALL | E_STRICT.

Turn all PHP warnings/errors into exceptions using something like the
following:

set_error_handler( create_function( ''


x,


这篇关于异常与错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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