php:try-catch没有捕获所有异常

时间:2022-10-28 13:22:30

I'm trying to do the following:

我正在尝试执行以下操作:

try {
    // just an example
    $time      = 'wrong datatype';
    $timestamp = date("Y-m-d H:i:s", $time);
} catch (Exception $e) {
    return false;
}
// database activity here

In short: I initialize some variables to be put in the database. If the initialization fails for whatever reason - e.g. because $time is not the expected format - I want the method to return false and not input wrong data into the database.

简而言之:我初始化一些要放在数据库中的变量。如果初始化因任何原因失败 - 例如因为$ time不是预期的格式 - 我希望该方法返回false而不是将错误的数据输入数据库。

However, errors like this are not caught by the 'catch'-statement, but by the global error handler. And then the script continues.

但是,像这样的错误不是由'catch'语句捕获的,而是由全局错误处理程序捕获的。然后脚本继续。

Is there a way around this? I just thought it would be cleaner to do it like this instead of manually typechecking every variable, which seems ineffective considering that in 99% of all cases nothing bad happens.

有没有解决的办法?我只是觉得这样做会更干净,而不是手动检查每个变量,考虑到在99%的情况下没有发生任何不良事件,这似乎是无效的。

3 个解决方案

#1


30  

Solution #1

Use ErrorException to turn errors into exceptions to handle:

使用ErrorException将错误转换为要处理的异常:

function exception_error_handler($errno, $errstr, $errfile, $errline ) {
    throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
set_error_handler("exception_error_handler");

Solution #2

try {
    // just an example
    $time      = 'wrong datatype';
    if (false === $timestamp = date("Y-m-d H:i:s", $time)) {
        throw new Exception('date error');
    }
} catch (Exception $e) {
    return false;
}

#2


6  

The shorter that I have found:

我找到的时间越短:

set_error_handler(function($errno, $errstr, $errfile, $errline ){
    throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
});

Makes all errors becoming instance of catchable ErrorException

使所有错误成为可捕获的ErrorException的实例

#3


0  

catch(Throwable $e) works catch ( Throwable $e){ $msg = $e-> getMessage(); }

catch(Throwable $ e)工作catch(Throwable $ e){$ msg = $ e-> getMessage(); }

#1


30  

Solution #1

Use ErrorException to turn errors into exceptions to handle:

使用ErrorException将错误转换为要处理的异常:

function exception_error_handler($errno, $errstr, $errfile, $errline ) {
    throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
set_error_handler("exception_error_handler");

Solution #2

try {
    // just an example
    $time      = 'wrong datatype';
    if (false === $timestamp = date("Y-m-d H:i:s", $time)) {
        throw new Exception('date error');
    }
} catch (Exception $e) {
    return false;
}

#2


6  

The shorter that I have found:

我找到的时间越短:

set_error_handler(function($errno, $errstr, $errfile, $errline ){
    throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
});

Makes all errors becoming instance of catchable ErrorException

使所有错误成为可捕获的ErrorException的实例

#3


0  

catch(Throwable $e) works catch ( Throwable $e){ $msg = $e-> getMessage(); }

catch(Throwable $ e)工作catch(Throwable $ e){$ msg = $ e-> getMessage(); }