Php 尝试catch抛出新异常,这是一个好的实践吗?
这是一个很好的练习吗?如果我执行此操作,是否会遇到问题?同时考虑单元测试Php 尝试catch抛出新异常,这是一个好的实践吗?,php,validation,exception,exception-handling,Php,Validation,Exception,Exception Handling,这是一个很好的练习吗?如果我执行此操作,是否会遇到问题?同时考虑单元测试 public function index($user) { try { $message = $this->_validate($user); } catch (MyCustomException $e) { $message = $e->getMessage(); } return $message; } private function
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;
}
这不是异常的预期用途,是一种不好的做法。例外情况适用于不可预见和/或当前开发人员无法控制的情况
在您的情况下,您可以预测一些用户将不会是“测试”用户,否则为什么要进行测试。您在这里所做的是使用一个异常来返回一条可选消息,然后您会回显该消息。因此,您不需要抛出异常,只需返回一条表明这一点的替代消息。这不是异常的预期用途,是不好的做法。例外情况适用于不可预见和/或当前开发人员无法控制的情况
在您的情况下,您可以预测一些用户将不会是“测试”用户,否则为什么要进行测试。您在这里所做的是使用一个异常来返回一条可选消息,然后您会回显该消息。因此,您不需要抛出异常,只需返回一条表明这一点的替代消息。这不是异常的预期用途,是不好的做法。例外情况适用于不可预见和/或当前开发人员无法控制的情况
在您的情况下,您可以预测一些用户将不会是“测试”用户,否则为什么要进行测试。您在这里所做的是使用一个异常来返回一条可选消息,然后您会回显该消息。因此,您不需要抛出异常,只需返回一条表明这一点的替代消息。这不是异常的预期用途,是不好的做法。例外情况适用于不可预见和/或当前开发人员无法控制的情况
在您的情况下,您可以预测一些用户将不会是“测试”用户,否则为什么要进行测试。您在这里所做的是使用一个异常来返回一条可选消息,然后您会回显该消息。因此,您不需要抛出异常,只需返回一条表明这一点的替代消息。当遇到无法处理的情况时,您应该只使用
抛出
,而不是使用抛出
来处理某些事情
在这种情况下,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
}
只有在遇到无法处理的情况时,才应
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
}
只有在遇到无法处理的情况时,才应
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
}
只有在遇到无法处理的情况时,才应
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
}
- 有些人会争辩说,当使用数据库时,如果存在潜在的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;
}
- 有些人会争辩说,当使用数据库时,如果存在潜在的sql错误,就应该捕获这些错误,并将它们记录下来/向用户提示
- 其他人则倾向于在try-catch中包装任何外部库
- 就我个人而言,我想用try-catch包装我的所有代码,并弹出异常(将它们一直抛出到控制器),在那里我将记录它们,如果发现它们,则以用户友好的格式处理输出