异常处理(Exceptions)
try {
//好好干,出了问题就抛出去
} catch (Exception $e) {
//时刻准备着,接 Exception !
} finally {
//怎么玩都逃不出我的手掌心
}设置自己的错误处理函数
Last updated
try {
//好好干,出了问题就抛出去
} catch (Exception $e) {
//时刻准备着,接 Exception !
} finally {
//怎么玩都逃不出我的手掌心
}Last updated
if(php_sapi_name() != 'cli') ob_start('nl2br');
function trytest() {
try {
echo "try\n";
//throw new Exception("I'm dying, Help!!");
//die;
return false;
} catch ( Exception $e ) {
echo $e->getMessage()."\n";
} finally {
echo "finally\n";
return true;
}
}
var_dump(trytest());try
finally
boolean trueWarning: file_get_contents() expects at least 1 parameter, 0 given in xxxxxset_error_handler (callable $handler [, int $error_types ]);// 可通过 set_error_handler + ErrorException 拦截php默认报错,转为异常
function trytest() {
try {
file_get_contents(); // parameter missing. w
} catch (Exception $e) {
echo $e->getMessage()."\n";
} finally {
return true;
}
}
function myErrorHandler($errno, $errstr, $errfile, $errline){
throw new ErrorException("Exception: ".$errstr, 0, $errno, $errfile, $errline);
}
set_error_handler("myErrorHandler");
trytest();//error_reporting(0);
register_shutdown_function(function() {
if ($error = error_get_last()) {
print_r($error);
}
});
// Fatal error: Call to undefined function test()
test();