Tag Archives: PHP record

PHP try will also throw an exception if it is executed successfully

PHP try will also throw an exception if it is executed successfully

try{
    Do something.
    Db::commit();
    return "success":
}cathe(\Exception $exception){
    Db::rollback();
    return "error":
}

Many of the returned results you get directly here, no matter whether your operation is successful or not, are returned as exceptions and go into error
Print exception error:

var_dump($e->getMessage());

The result is

"" 

There is no information
at this time, it should be noted that the return in the exception try will be thrown as an exception. There are two solutions.

First:
will

catch (\Exception $exception)

Change to (eg: use tp6, please rewrite the specific path within the framework)

catch (\think\Exception\DbException $exception)

Second:
save the information that needs to return in the variable, and finally output:

$_retData = [];
try{
    Do something.
    Db::commit();
    $_retData['data'] = [];
    $_retData['messages'] = "success";
}cathe(\Exception $exception){
    Db::rollback();
    return "error":
}
return $_retData;