在PHP中使用Try-catch结构

Using Try-catch structure in PHP

本文关键字:Try-catch 结构 PHP      更新时间:2023-09-26

我以前从未在代码中使用过Try-catch,但现在我需要这样做,而且我似乎不太了解它的工作方式。我有一个字符串中的数据,我使用爆炸:

$groupNumbers = array();
$str = $dataGroups['groups'];
$groupNumbers = explode(",", $str);
$count = count($groupNumbers);

然后我想检查每个元素是否都是数字,如果是,我将继续数据库查询,否则我想中止操作并返回一些错误。

以下是我正在做的:

for ($i = 0; $i < $count; ++$i)
    {
        try
        {
            is_numeric($groupNumbers[$i]);
        }
        catch (Exception $ex)
        {
            process_exception_to_json($ex);
        }
    }

如果每个元素都是数字,我会构造一个活动记录来执行SQL:

$this->db->insert_batch('users_groups',$data);

显然是这样写的,即使元素不是数字,操作也不会中止,而且insert_batch仍然使用未验证的值执行,这是我想要避免的。具体的方法是什么,这样我就可以获得一个异常,同时中止操作。感谢

Leron

这是因为is_numericDocs不会抛出异常,而是返回一个值。它永远不会失败/抛出异常。

你需要这样做(只是一个例子,我不建议这样做,因为这是多余的):

try
{
    if (!is_numeric($groupNumbers[$i])) {
        throw new RuntimeException('Not numeric.');
    }
}
catch (Exception $ex)
{
    process_exception_to_json($ex);
}

相反,创建您自己的组号类型:

try
{
    $groupNumbers = GroupNumbers::createFromString($dataGroups['groups']);
}
catch (Exception $ex)
{
    process_exception_to_json($ex);
}

具有以下类型:

class GroupNumbers extends ArrayObject
{
    public function construct(Array $numbers) {
        foreach ($numbers as $number)
        {
            if (!is_numeric($number))
            {
                throw new InvalidArgumentException(sprintf('Not numeric "%s".', $number));
            }
        }
        parent::__construct($numbers);
    }
    public static function createFromString($string) {
        return new self(explode(",", $string));
    }
}

字符串处理现在被封装,不再有外部循环,并且只有当字符串中有实际数字时,GroupNumbers才会实例化。

如果你对类不熟悉,那么使用过程式风格也可以获得类似的好处。这可能更容易理解(但基本相同):

try
{
    $groupNumbers = GroupNumbers_createFromString($dataGroups['groups']);
}
catch (Exception $ex)
{
    process_exception_to_json($ex);
}
function GroupNumbers_createFromString($string)
{
    $numbers = explode(",", $string);
    foreach ($numbers as $number)
    {
        if (!is_numeric($number))
        {
            throw new InvalidArgumentException(sprintf('Not numeric "%s".', $number));
        }
    }
    return $numbers;
}

is_numeric从不抛出任何异常,因此您的示例不会执行任何操作,这将适用于

for ($i = 0; $i < $count; ++$i)
{
    try
    {
        if (is_numeric($groupNumbers[$i]) !== true)
        {
            throw new Exception($groupNumbers[$i] . ' is not numeric');
        }
    }
    catch (Exception $ex)
    {
        process_exception_to_json($ex);
    }
}

以下将完成(几乎)相同的事情:

for ($i = 0; $i < $count; ++$i)
{
    if (is_numeric($groupNumbers[$i]) !== true)
    {
        process_exception_to_json($groupNumbers[$i] . ' is not numeric');
    }
}

如果你想验证,你不需要在这里使用Exception

for ($i = 0; $i < $count; ++$i)
{
    if((int) $groupNumbers[$i] <= 0) {
        process_exception_to_json($groupNumbers[$i]);
        break; //optinal
    }
}

您需要做一些事情,在try块中实际抛出异常。is_numeric()只会返回false,所以这不起作用。这将:

try {
    if (!is_numeric($groupNumbers[$i]) {
        throw new exception('Uh-oh!');
    } 
} catch (Exception $ex) {
    process_exception_to_json($ex);
}

哪一个可能最好这样做:

function checkNumbers($groupNumbers) {
    for ($i = 0; $i < count($groupNumbers); ++$i) {
        if (!is_numeric($groupNumbers[$i]) {
            throw new Exception('"'.$groupNumbers[$i]." is non-numeric!");
        } 
    }
}
try {
    checkNumbers($numbers);
    // Won't get this far if it throws an exception
    $this->db->insert_batch('users_groups', $numbers);
} catch (Exception $ex) {
    process_exception_to_json($ex);
}

异常可以非常深入地嵌套在代码中,主要的好处是错误消息是特定的,并且会弹出,而不必担心大量的真值或假值。您可能有几十个从checkNumbers()调用的链式函数,无论发生什么错误,它都会弹出在catch块中,准备将其发送到JSON。