php中未处理的错误

lex*_*xus 6 php error-handling

如何在运行时知道我的代码发出了警告?

try {
    echo (25/0);
} catch (exception $exc) {
    echo "exception catched";
}
Run Code Online (Sandbox Code Playgroud)

抛出一个"警告:除零"错误,我无法处理我的代码.

sve*_*ens 7

你正在寻找这个功能set_error_handler().查看手册中的示例代码.

确保您不仅要禁止错误警告,而且要将它们静默地重定向到日志文件或类似的东西.(这有助于您追踪错误)


You*_*ung 5

您需要自己处理异常,如下所示

function inverse($x)
{
    if(!$x)
    {
         throw new Exception('Division by zero.');
    }
    else
    { 
        return 1/$x;
    }
}


try
{
     echo inverse(5);
     echo inverse(0);
}
catch (Exception $e)
{ 
    echo $e->getMessage();
}
Run Code Online (Sandbox Code Playgroud)