Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP捕获未使用自定义异常处理程序捕获异常_Php_Exception_Error Handling_Try Catch_Custom Error Handling - Fatal编程技术网

PHP捕获未使用自定义异常处理程序捕获异常

PHP捕获未使用自定义异常处理程序捕获异常,php,exception,error-handling,try-catch,custom-error-handling,Php,Exception,Error Handling,Try Catch,Custom Error Handling,我遇到了一个问题,当我使用Try-Catch块时,我的PHP项目没有捕获异常。当抛出异常时,PHP仍将显示异常的错误消息,并将结束PHP脚本的执行。我已经把范围缩小到我有一个定制的PHP错误处理程序。一旦我删除了我的定制PHP处理程序,然后尝试CATCH块再次正常工作。不幸的是,删除自定义错误处理程序不是一个选项。请参见下面的代码: use \ErrorException; use \BrowserDetection; use \DateTime; use \D

我遇到了一个问题,当我使用Try-Catch块时,我的PHP项目没有捕获异常。当抛出异常时,PHP仍将显示异常的错误消息,并将结束PHP脚本的执行。我已经把范围缩小到我有一个定制的PHP错误处理程序。一旦我删除了我的定制PHP处理程序,然后尝试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("Base\Handlers\ErrorHandler", "error_handler"), E_ERROR | E_PARSE);
                            break;
                        case 2:
                            error_reporting(E_ERROR | E_WARNING | E_PARSE);
                            set_error_handler(array("Base\Handlers\ErrorHandler", "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("Base\Handlers\ErrorHandler", "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("Base\Handlers\ErrorHandler", "error_handler"), E_STRICT | E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
                            break;
                        default:
                            error_reporting(E_ALL);

                            set_error_handler(array("Base\Handlers\ErrorHandler", "error_handler"), E_ALL);
                            register_shutdown_function(array("Base\Handlers\ErrorHandler", "fatal_error_handler"));
                    }
                    set_exception_handler(array("Base\Handlers\ErrorHandler", "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) == "Base\Handlers\CoreErrorException") {   
                $sTraceArray = array_slice(explode("\n", $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 . "\n</p>" . PHP_EOL;;
            $message .=  '<p><b>' . str_repeat("-", 75) . ' PHP STACK TRACE ' . str_repeat("-", 75) . '</b>' . PHP_EOL;
            $message .= str_replace('#', '<br />#', $traceString) . "</p>\n". 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' . "\r\n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
            $headers .= 'From: ' . $companyName .' <reports@forbin.com>' . "\r\n";
            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'; 
        }
    }
    ?>
类调用异常

<?php
namespace Base\Members\Library; 
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; 
    }
}
GetAllOrdersByOrderNo($request);
}捕获(例外$e){
//此代码从未设置
$response=null;
}
/*if(response.OperationStatus!=Orders.externalservicesponsestatus.Success)
{
抛出新异常(response.OperationErrors[0]);
} */ 
返回isset($response->GetAllorderByOrderNoresult->Orders->Order)$响应->GetAllOrdersByOrderNoResult->订单->订单:空;
}
}
您位于命名空间Base\Members\Library中

该块正在捕获Base\Members\Library\Exception

<?php
namespace Base\Members\Library; 
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; 
    }
}

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

引发新异常的
GetAllOrdersByOrderNo
实现在哪里(
引发新异常(“某些错误”)
)?它抛出的类是否实现了
\Exception
,因为它在PHP中不是必需的,所以如果它不是
\Exception
的子类,则无法捕获异常,因为
带有
catch(\expetion$e)
。我已对其进行了编辑并将其添加到中供您查看。但是,即使在此代码之外,也无法捕获任何异常,即使它位于全局命名空间中。您不会从SoapClient类RPC调用中获取PHP异常,因为SoapClient没有异常,例如
$client->GetAllOrdersByOrderNo($request)
永远不会抛出异常,即使远程系统抛出异常,如果它应该将异常或错误输出给您,那么您必须解析结果以检查它是否处于错误状态。还要注意的是,PHP编程错误不是PHP中的例外,它们是错误,您能否提供失败状态响应的演示
echo”“.print_r($response,true)。”我可以,但我认为你对SoapClient类的关注太多了。这只是一个例子,任何抛出异常的类都不能被try-catch捕获。所以,如果我只是抛出一个新的异常(“Hello World”),它仍然不会被捕获。这就解决了问题。谢谢
 try {
     $response = $client->GetAllOrdersByOrderNo($request);
 } catch (Exception $e) {
     // This code is never set
     $response = null; 
 }