我想在这个PHP脚本中抛出异常或错误? [英] Would I want to throw an exception or an error in this PHP script?

查看:245
本文介绍了我想在这个PHP脚本中抛出异常或错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行数据库查询的PHP脚本。现在,如果查询失败,应该触发错误还是抛出异常?我注意到,如果我做了后者,脚本执行将在面临异常之后停止。



我的代码如下:

  if(!$ this-> connection [0]  - > query($ this-> query))
throw new Exception($ this- >连接[0] - >错误);

在这种情况下(失败的查询)使用异常有什么利弊? p>

解决方案

取决于您的一般错误处理策略和传递给此函数的查询。投掷异常本身就是一个很好的主意,嗯,如果被抓到某个地方并被处理。


I have a PHP script that runs database queries. Now, if a query fails, should I trigger an error or throw an exception? I noticed that if I do the latter, the script execution will stop after facing the exception.

My code is as follows:

if (!$this->connection[0]->query($this->query))
    throw new Exception($this->connection[0]->error);

What are the pros and cons of using exceptions for this kind of cases (failed queries)?

解决方案

depends on your general error handling strategy and the queries passed to this function. Throwing Exceptions itself is a very good idea, IMHO, if they are caught somewhere and processed.

这篇关于我想在这个PHP脚本中抛出异常或错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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