PHP 7尝试 – 捕获:无法捕获“可捕获的致命错误”
发布时间:2020-12-13 16:08:47 所属栏目:PHP教程 来源:网络整理
导读:我正在玩try – catch块: ?phptry { $str = "http://rejstrik-firem.kurzy.cz/73631604"; $domOb = new DOMDocument(); $html = $domOb-loadHTMLFile($str); $domOb-preserveWhiteSpace = false; $container = $domOb-getElementById('ormaininfotab'); ech
我正在玩try – catch块:
<?php try { $str = "http://rejstrik-firem.kurzy.cz/73631604"; $domOb = new DOMDocument(); $html = $domOb->loadHTMLFile($str); $domOb->preserveWhiteSpace = false; $container = $domOb->getElementById('ormaininfotab'); echo $container; // <========= this is intended error which I want catch } catch (Exception $e) { echo "Exception" . $e->getMessage() . ". File: " . $e->getFile() . ",line: " . $e->getLine(); } catch (Error $e) { echo "Error" . $e->getMessage() . ". File: " . $e->getFile() . ",line: " . $e->getLine(); } ?> 我的结果如下:
为什么第二次捕获没有捕获到这个错误? 解决方法
作为
user2782001 mentioned,这不是PHP开发人员眼中的错误.他们甚至指出这些类型的错误应该被称为“可恢复的”:
在ErrorException manual page上有一个简洁的解决方法,将那些“可捕获/可恢复”错误转换为ErrorException. <?php function exception_error_handler($severity,$message,$file,$line) { if (!(error_reporting() & $severity)) { // This error code is not included in error_reporting return; } throw new ErrorException($message,$severity,$line); } set_error_handler("exception_error_handler"); ?> 现在,您将能够捕获这些错误: <?php try { // Error code } catch (Error $e) { // this will catch only Errors echo $e->getMessage(); } ?> 要么 try { // Error code } catch (Throwable $t) { // this will catch both Errors and Exceptions echo $t->getMessage(); } ?> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |