PHP Catch没有捕获自定义异常处理程序的异常

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP Catch没有捕获自定义异常处理程序的异常相关的知识,希望对你有一定的参考价值。

我遇到了一个问题,当我使用Try Catch Block时,我的php项目没有捕获异常。抛出异常时,PHP仍将显示异常的错误消息,并将结束PHP脚本的执行。我已经缩小到我已经构建了一个自定义PHP错误处理程序的事实。删除自定义PHP处理程序后,TRY CATCH块再次正常工作。不幸的是,删除自定义错误处理程序不是一个选项。请参阅以下代码:

    use ErrorException; 
    use BrowserDetection;
    use DateTime;
    use DateTimeZone; 
    use Database; 

    define("DEBUG_ON", "DEBUG_ON");
    define("DEBUG_OFF", "DEBUG_OFF");

    ErrorHandler::register(); 

    class ErrorHandler
    {
        public static function register() {
            global $_WEBCONFIG; 
            if(!isset($_COOKIE['XDEBUG_SESSION'])) {
                if($_WEBCONFIG['DEBUG_SETTING'] == DEBUG_OFF || stristr($_SERVER['PHP_SELF'], 'error.php')) {
                    ini_set('display_errors', '0');
                    error_reporting(0);
                } else {
                    ini_set('display_errors', '1');
                    switch ($_WEBCONFIG['DEBUG_WARNING_LEVEL']) {
                        case 1:
                            error_reporting(E_ERROR | E_PARSE);
                            set_error_handler(array("BaseHandlersErrorHandler", "error_handler"), E_ERROR | E_PARSE);
                            break;
                        case 2:
                            error_reporting(E_ERROR | E_WARNING | E_PARSE);
                            set_error_handler(array("BaseHandlersErrorHandler", "error_handler"), E_ERROR | E_WARNING | E_PARSE);
                            break;
                        case 3:
                            error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
                            set_error_handler(array("BaseHandlersErrorHandler", "error_handler"), E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
                            break;
                        case 4:
                            error_reporting(E_STRICT | E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
                            set_error_handler(array("BaseHandlersErrorHandler", "error_handler"), E_STRICT | E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
                            break;
                        default:
                            error_reporting(E_ALL);

                            set_error_handler(array("BaseHandlersErrorHandler", "error_handler"), E_ALL);
                            register_shutdown_function(array("BaseHandlersErrorHandler", "fatal_error_handler"));
                    }
                    set_exception_handler(array("BaseHandlersErrorHandler", "exception_handler"));
                }
            }
        }

        public static function log_exception($exception) {
            require_once($_SERVER['DOCUMENT_ROOT'] . "/library/classes/class_browser_detection.php");
            global $_WEBCONFIG, $siteConfig;

            $browser = new BrowserDetection();
            $userBrowserName = $browser->getBrowser();
            $userBrowserVer = $browser->getVersion();
            $userBrowserPlatform = $browser->getPlatform();
            $userBrowserIsMobile = $browser->isMobile() == "" ? "No" : "Yes";
            $userBrowserIsRobot = $browser->isRobot() == "" ? "No" : "Yes";
            $companyName = isset($siteConfig['Company_Name']) ? $siteConfig['Company_Name'] : '';
            $userAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Unknown';
            $ip = isset($_SERVER['HTTP_CLIENT_IP']) ? $_SERVER['HTTP_CLIENT_IP'] : 'N/A';
            $today = new DateTime("now", new DateTimeZone('US/Central'));
            $date = $today->format("F j, Y g:i a"); 
            $username = isset($_SESSION['member_username']) ? $_SESSION['member_username'] : (isset($_SESSION['user_username']) ? $_SESSION['user_username'] : null); 

            // Remove ErrorException Trace Referenceing Error Handler & Get Error Type
            if(get_class($exception) == "BaseHandlersCoreErrorException") {   
                $sTraceArray = array_slice(explode("
", $exception->getTraceAsString()), 1);
                $traceString = implode($sTraceArray); 
                $type = $exception->getExceptionType(); 
            } else {
                $traceString = $exception->getTraceAsString(); 
                $type = 'E_' . strtoupper(get_class($exception));
            }

            $message = '<p><b>EXCEPTION TYPE</b>: ' . $type . PHP_EOL;  
            $message .= '<br /><b>MESSAGE: </b>' . $exception->getMessage(). PHP_EOL;
            $message .=   '<br /><b>SITE PAGE: </b>' . $_SERVER['ABSOLUTE_URI']. PHP_EOL;
            $message .=  '<br /><b>LOCATION: </b>' . $exception->getFile(). PHP_EOL;
            $message .= '<br /><b>LINE NUMBER: </b>' . $exception->getLine(). PHP_EOL;
            if(!is_null($username)) {
                $message .=  '<br /><b>USERNAME: </b>' . $username . PHP_EOL;
            }
            $message .=  '<br /><b>USER AGENT: </b>' . $userAgent . PHP_EOL;
            $message .=  '<br /><b>OPERATING SYSTEM: </b>' . $userBrowserPlatform . PHP_EOL;
            $message .=  '<br /><b>BROWSER: </b>' . $userBrowserName . ' ' . $userBrowserVer . PHP_EOL;
            $message .=  '<br /><b>MOBILE: </b>' . $userBrowserIsMobile . PHP_EOL;
            $message .=  '<br /><b>ROBOT: </b>' . $userBrowserIsRobot . PHP_EOL;
            $message .=  '<br /><b>IP ADDRESS: </b>' . $ip  . PHP_EOL;
            $message .=  '<br /><b>ERROR TIME: </b>' . $date . "
</p>" . PHP_EOL;;
            $message .=  '<p><b>' . str_repeat("-", 75) . ' PHP STACK TRACE ' . str_repeat("-", 75) . '</b>' . PHP_EOL;
            $message .= str_replace('#', '<br />#', $traceString) . "</p>
". PHP_EOL;;

            if(Database::hasConnection()) {
                $trace = Database::quote_smart($traceString);
                $filepath = Database::quote_smart($exception->getFile());
                $errLine = (int) $exception->getLine();
                $errNo = (int) $exception->getCode();
                $errStr = Database::quote_smart($exception->getMessage());
                if(!is_null($username)) {
                    if(isset($_SESSION['member_prov_num'])) {
                       $providerNo = $_SESSION['member_prov_num']; 
                       $sql = "INSERT INTO `tbl_error_log` (`el_type`, `el_code`, `el_message`, `el_url`, `el_file_path`, `el_line_number`, `el_user_agent`, `el_ip_address`, `el_details`, `el_username`, `el_provider_number`)
                               VALUES ('$type', '$errNo', '$errStr', '{$_SERVER['ABSOLUTE_URI']}', '$filepath', '$errLine', '$userAgent', '$ip', '$trace', '$username', '$providerNo');";
                    } else {
                        $sql = "INSERT INTO `tbl_error_log` (`el_type`, `el_code`, `el_message`, `el_url`, `el_file_path`, `el_line_number`, `el_user_agent`, `el_ip_address`, `el_details`, `el_username`)
                            VALUES ('$type', '$errNo', '$errStr', '{$_SERVER['ABSOLUTE_URI']}', '$filepath', '$errLine', '$userAgent', '$ip', '$trace', '$username');";
                    }
                } else {
                    $sql = "INSERT INTO `tbl_error_log` (`el_type`, `el_code`, `el_message`, `el_url`, `el_file_path`, `el_line_number`, `el_user_agent`, `el_ip_address`, `el_details`)
                            VALUES ('$type', '$errNo', '$errStr', '{$_SERVER['ABSOLUTE_URI']}', '$filepath', '$errLine', '$userAgent', '$ip', '$trace');";
                }
                Database::ExecuteRaw($sql);
            }

            $headers  = 'MIME-Version: 1.0' . "
";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "
";
            $headers .= 'From: ' . $companyName .' <reports@forbin.com>' . "
";
            if($_WEBCONFIG['DEBUG_SETTING'] == DEBUG_ON) {
                $recipients = str_replace(";",",",$_WEBCONFIG['DEVELOPER_EMAIL']);
                mail($recipients, 'Error Message', $message, $headers);
            }
            return $message; 
        }

        public static function error_handler($errNo, $errStr, $errFile, $errLine)
        {
            // Convert PHP Error Into Exception
            self::exception_handler(new CoreErrorException($errStr, $errNo, $errFile, $errLine));
        }

        public static function fatal_error_handler() {
            $last_error = error_get_last();
            if(isset($last_error['type']) 
                || $last_error['type'] ===  E_ERROR 
                || $last_error['type'] ===  E_CORE_ERROR
                || $last_error['type'] ===  E_CORE_WARNING
                || $last_error['type'] ===  E_COMPILE_ERROR
                || $last_error['type'] ===  E_COMPILE_WARNING) {
                self::exception_handler(new CoreErrorException($last_error['message'], $last_error['type'], $last_error['file'], $last_error['line']));
            } 
        } 

        public static function exception_handler($exception) {

            global $_WEBCONFIG;

            $message = self::log_exception($exception); 
            if($_WEBCONFIG['FRIENDLY_ERRORS'] == "Off" || ($_WEBCONFIG['FRIENDLY_ERRORS'] == "Local" && ($ip == $_WEBCONFIG["LOCAL_IP"] || '127.0.0.1'))) {
                header_remove('content-type'); 
                header('content-type: text/html; charset=UTF-8');
                if (ob_get_length() > 0) { ob_end_clean(); }
                self::print_header();
                print $message;
                self::print_footer();
                exit();
            } else {
                $errorPage = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : '';
                redirect($_WEBCONFIG['CUSTOMERRORS']['DEFAULTREDIRECT']['500'] . '?path=' . $errorPage);
            }
        }

        private static function print_header() {
            global $_WEBCONFIG, $siteConfig;

            print '<!doctype html><html xmlns="http://www.w3.org/1999/xhtml"><head><title>PHP Page Error</title>';
            print '<style>body { background-color: #fcfcfc; color: #333333; margin: 0; padding:0; } h1 { font-size: 1.5em; font-weight: normal; background-color: #9999cc; min-height:2em; line-height:2em; border-bottom: 1px inset black; margin: 0; } h1, p { padding-left: 10px; } code.url { background-color: #eeeeee; font-family:monospace; padding:0 2px;}</style>';
            print '</head><body>';
            print '<div class="content" style="left: 50px;">';
            print "<h1>PHP Page Error</h1>";

        }

        private static function print_footer() {
            print '</div></body></html>';
        }
    }

    class CoreErrorException extends ErrorException {

        protected $errorType; 

        public function __construct($errStr, $errNo, $errFile, $errLine) {
            parent::__construct($errStr, 0, $errNo, $errFile, $errLine);
            switch ($this->code) {
                case E_ERROR:
                    $this->errorType = "E_ERROR";
                    break; 
                case E_WARNING :
                    $this->errorType = "E_WARNING";
                    break; 
                case E_PARSE :
                    $this->errorType = "E_PARSE";
                    break; 
                case E_NOTICE:
                    $this->errorType = "E_NOTICE";
                    break; 
                case E_CORE_ERROR :
                    $this->errorType = "E_CORE_ERROR";
                    break; 
                case E_CORE_WARNING :
                    $this->errorType = "E_CORE_WARNING";
                    break; 
                case E_COMPILE_ERROR:
                    $this->errorType = "E_COMPILE_ERROR";
                    break; 
                case E_COMPILE_WARNING :
                    $this->errorType = "E_COMPILE_WARNING";
                    break; 
                case E_USER_ERROR :
                    $this->errorType = "E_USER_ERROR";
                    break; 
                case E_USER_WARNING:
                    $this->errorType = "E_USER_WARNING";
                    break; 
                case E_USER_NOTICE :
                    $this->errorType = "E_USER_NOTICE";
                    break; 
                case E_STRICT :
                    $this->errorType = "E_STRICT";
                    break; 
                case E_RECOVERABLE_ERROR:
                    $this->errorType = "E_RECOVERABLE_ERROR";
                    break; 
                case E_DEPRECATED :
                    $this->errorType = "E_DEPRECATED";
                    break; 
                case E_USER_DEPRECATED :
                    $this->errorType = "E_USER_DEPRECATED";
                    break; 
                default:
                    $this->errorType = "E_CORE";
            }
        }
        public function getExceptionType() {
            return $this->errorType . '_EXCEPTION'; 
        }
    }
    ?>

我还包含了一个catch块的示例,其中没有捕获代码:

    try {
        $response = $client->GetAllOrdersByOrderNo($request);
    } catch (Exception $e) {
        $response = null; 
    }

类调用异常

<?php
namespace BaseMembersLibrary; 
use SoapClient; 
use SimpleXMLElement; 

class MembershipAPI
{
    public static function getOrderByOrderNo($orderNo) { 
        $client = new SoapClient("https://bah.foo.com/members/Services/Orders.svc?wsdl");
        $request = array(array()); 
        $request['request']['OrderNo'] = $orderNo; 
        try {
            $response = $client->GetAllOrdersByOrderNo($request);
        } catch (Exception $e) {
            // This code is never set
            $response = null; 
        }

        /*if (response.OperationStatus != Orders.ExternalServiceResponseStatus.Success)
        {
            throw new Exception(response.OperationErrors[0]);
        } */ 
        return isset($response->GetAllOrdersByOrderNoResult->Orders->Order) ? $response->GetAllOrdersByOrderNoResult->Orders->Order : null; 
    }
}
答案
 try {
     $response = $client->GetAllOrdersByOrderNo($request);
 } catch (Exception $e) {
     // This code is never set
     $response = null; 
 }

您位于名称空间Base Members Library中。

该块正在捕获Base Members Library Exception

尝试在全局命名空间中捕获 Exception。

以上是关于PHP Catch没有捕获自定义异常处理程序的异常的主要内容,如果未能解决你的问题,请参考以下文章

java中Debug调试异常的概念异常体系处理异常的关键字try/catch/finally/throw/throws多个异常使用捕获并处理的方式继承关系中处理异常自定义异常类

java中Debug调试异常的概念异常体系处理异常的关键字try/catch/finally/throw/throws多个异常使用捕获并处理的方式继承关系中处理异常自定义异常类

java自定义异常(Exceptionthrowstry-catch)

Java中的自定义异常捕获方式

php try catch 捕获哪些错误

catch(...) 没有捕获异常,我的程序仍然崩溃