PHP和例外。你的意见? [英] PHP and exceptions. Your opinion?

查看:61
本文介绍了PHP和例外。你的意见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。


我是C#程序员,所以我有点习惯使用Exceptions,而且我正在开发一个PHP项目,我'用.Net风格编写,使用Exceptions,对象和命名样式,就像在C#中一样。


原始PHP函数或对象都没有使用Exception系统。它们被编写为返回错误代码或布尔值,而不是抛出异常。

这显然是编码PHP的默认方式,我怀疑很多人使用Exception系统,除了'' C喜欢像我这样的编码人员,如果我以外的其他人使用我的代码会产生问题。


我只是在徘徊你们的想法。

有人使用除我以外的例外情况吗?我应该休息并尝试遵循PHP的做事方式吗?


更重要的是;有没有明显的优点/缺点使用你知道的例外?

解决方案

不,PHP没有使用异常的唯一原因是PHP没有'' t 例外。虽然到目前为止它主要是由经验丰富的程序员保留(因为它是语言的新手),但我强烈建议使用它们。使用它们没有明显的缺点,除非你没有发现异常,然后你有一个未被捕获的例外。


但是,我没有看到你离开它们未捕获。 ;)


我不是粉丝,因为他们违反了这么多规则。


首先,当你抛出异常时,你的代码停止,并且catch块(可能位于抛出异常的过程之外)被执行。这对我来说没有意义,因为异常恢复应该在异常的情况下处理(Demeter的定律和OOP背后的基本前提)。


例如:

展开 | 选择 | Wrap | 行号


例外对于致命错误而言比其他事情更多。说这个代码可以在这个函数失败后工作,但是没有回来。 :P

Hi.

I''m a C# programmer by nature so I''m kind of used to using Exceptions and I''m currently working on a PHP project that I''ve written in a .Net style, using Exceptions ,objects and naming styles like I would in C#.

None of the original PHP functions or objects use the Exception system. They are written to return error codes or boolean values rather then throw exceptions.
This is obviously the ''default'' way of coding PHP, and I doubt many people use the Exception system, except ''C like'' coders like myself, which would create problems if someone other than myself were to use my code.

I''m just wandering what you guys think.
Does anybody use Exceptions other than me? Should I just give it a rest and try to follow the PHP way of doing things?

And more importantly; is there any clear advantage/disadvantage of using Exceptions that you know of?

解决方案

No, the only reason PHP didn''t use exceptions is because PHP didn''t have exceptions. Though it is mostly reserved to the more experienced programmers so far (as it is new to the language), I highly recommend their use. There are no clear disadvantages to their use other than if you don''t catch the exception, then you have an uncaught exception.

However, I don''t see you leaving them uncaught. ;)


I''m not a fan because they break so many rules.

First off, when you throw an exception, your code halts, and the catch block (which might be located outside the procedure that threw the exception) gets executed. This doesn''t make sense to me, because exception recovery should be handled within the context of that exception (Demeter''s Law and the basic premise behind OOP).

For example:

Expand|Select|Wrap|Line Numbers


Exceptions are meant more for fatal errors than other things. Saying that this code could work EXCEPT once this function has failed, there''s no coming back. :P


这篇关于PHP和例外。你的意见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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