这是一个很好的实践吗?如果我执行此操作,我会遇到问题吗?同时考虑单元测试。
public function index($user)
{
try {
$message = $this->_validate($user);
} catch (MyCustomException $e) {
$message = $e->getMessage();
}
return $message;
}
private function _validate($user)
{
if ($user != 'test') throw new MyCustomException('Invalid User');
return 'Valid User';
}
更新(添加另一个示例)如果我从SQL中检索数据,那么当没有检索到记录时,抛出一个异常。
public function retrieveData($args)
{
$codeTable = $model->getCodeTables('code_tables', $args);
if (empty($codeTable)) throw new MyCustomException($this->getCustomMessage()['sysMsgDataDoesNotExists']);
// more codes here
return $codeTable;
}
public function index($args)
{
try {
$this->retrieveData($args);
// if data retrieve is not empty, execute codes here like insert, delete, update other records or other fancy logic
$message = 'success';
} catch (MyCustomException $e) {
$message = $e->getMessage();
}
return $message;
}
在第二个例子中,我的目标是在没有检索到数据的情况下立即进行捕获。而不是我做这样的事。
public function retrieveData($args)
{
$codeTable = $model->getCodeTables('code_tables', $args);
if (empty($codeTable)) return $this->getCustomMessage()['sysMsgDataDoesNotExists'];
// more codes here
return $codeTable;
}
public function index($args)
{
$data = $this->retrieveData($args);
if (is_array($data)) {
// if data retrieve is not empty, execute codes here like insert, delete, update other records or other fancy logic
$message = 'success';
} else {
$message = $data;
}
return $message;
}
当遇到无法处理的情况时,应该只使用throw
,而不是使用throw
来处理某些事情。
在这种情况下,true
或false
的标志是合适的。
public function index($user)
{
return isValid($user) ? 'Valid user' : 'Invalid user';
}
private function isValid($user)
{
return $user === 'test';
}
例如,如果您编写的函数需要传递一个参数,而该参数没有传递,那么throw
是有意义的。这意味着开发人员忘记了传递它,让他知道的最好方法是抛出,这样一切都停止了。
function foo($a, $b) {
if (!$a || !$b) {
throw new Exception('Gotta have parameters, dude!');
}
// etc
}
这不是异常的预期用途,也是一种糟糕的做法。例外情况适用于不可预见和/或当前开发商无法控制的情况。
在你的情况下,你可以预测一些用户不会是"测试"用户,否则为什么要进行测试。您在这里所做的是使用一个异常来返回一条备选消息,然后进行回显。因此,您不需要抛出异常,只需返回一条表明这一点的替代消息即可。
对于其他示例,我想这取决于您真正查询的内容的上下文。
例如:
-
如果查询是某种搜索,那么可能没有得到任何结果。包装器/orm应该只返回一个空数组,并将其视为正常情况。您可以检查行数并决定要做什么。
-
如果查询类似于"我在这一天应该使用的税率是多少",并且应用程序应该在使用之前预装信息,那么这可能是出现异常的原因。但仅来自类似
get_tax_rate()
的函数,而不是来自通用数据库查询代码。(即使这样,如果有一些用户暴露的用于定义税率的页面,并且"我没有税率,请定义一个"是有效的错误消息,则可能不需要例外) -
任何您无法真正控制的情况,如断开连接、超时、获取不符合模型的数据等,都是引发异常的原因。
-
有些人会争辩说,当使用数据库并出现潜在的sql错误时,应该抓住这些错误并将其记录/冒泡给用户。
-
其他人则倾向于将任何外部库包装在try-catch中。
-
就我个人而言,我想把我的所有代码都包装在try-catch中,并弹出异常(将它们一直扔到控制器),在那里我会记录它们,如果发现它们,则以用户友好的格式处理输出。
要点-如果发生任何不可预测的事情,它们只是一种以优雅的方式脱离逻辑的方法。
下面是一个如何将索引包装为始终一致的示例:
public function index( $args )
{
// Init return.
$aReturn = array(); // Or object.
try
{
$aFetch = $this->retrieveData( $args );
if (!empty( $aFetch) )
{
$aReturn = $aFetch;
}
}
catch( Exception $oException )
{
// Optionally log exception or do nothing.
// log( $oException );
}
// Return will always be an array either with data or empty.
return $aReturn;
}