我们需要什么PHP代码?任何用例情况? [英] What do we need the PHP-exception code for? Any use case scenario?

查看:123
本文介绍了我们需要什么PHP代码?任何用例情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这是一个非常跛脚的问题,但是我希望我有压倒性的回应:)



当我在PHP中抛出异常时,我可以添加一个代码消息。

我捕获一个异常并根据其类型处理它(像 InvalidArgumentException OutOfBoundException )。我记录消息或显示它或做任何适合的。

我可以添加还附加一个以前的异常来跟踪错误原点的路径。



但是我从未使用或从未想过的一件事是:代码有用?



例如:

  throw new Exception(db Error,$ code,$ previousException ); 

如何使用 $ code

解决方案

该消息用于向用户显示,而代码由您的程序使用。例如,在数据库错误示例中,您可能会组成一组代码,如


  1. 无法连接

  2. 查询期间出错

  3. 空结果

  4. 关闭连接时出错

然后使用相应的代码。那么当你的代码的其他部分看到他们是异常的时候,他们会知道发生了什么,并且可以智能地处理它。


Okay, its a very lame question for many but I hope I will have overwhelming response :)

When I throw an Exception in PHP I can add a code to the message.
I catch an exception and handle it according to its type (Like InvalidArgumentException or OutOfBoundException). I log the message or display it or do whatever is suitable.
I can add also append a previous exception to trace a path to the origin of the error.

BUT one thing I have never used or never thought of: how useful is code?

For example:

throw new Exception("db Error", $code, $previousException);

What do I do with $code?

解决方案

The message is for display to the user, while the code is for use by your program. So for example, in your "database error" example, you might make up a set of codes like

  1. Can't connect
  2. Error during query
  3. Empty result
  4. Error closing connection

and then use the appropriate code. Then when other parts of your code saw they exception, they would know what happened and could possibly deal with it intelligently.

这篇关于我们需要什么PHP代码?任何用例情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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