亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
  • 查看全部
  • /**
    ?*?方式二:自定義異常類
    ?*?Class?ErrorToException
    ?*/
    //顯示所有的錯誤
    error_reporting(-1);
    class?ErrorToException?extends?Exception{
    ????public?static?function?handle($errno,$errstr)
    ????{
    ????????throw?new?self($errstr,0);
    ????}
    }
    
    set_error_handler(array('ErrorToException','handle'));
    set_error_handler(array('ErrorToException','handle'),E_USER_WARNING|E_WARNING);
    
    try{
    ????echo?$test;//notice,不會被處理
    ????echo?gettype();//warning
    ????//手動觸發錯誤
    ????trigger_error('test',E_USER_WARNING);
    }catch?(Exception?$exception){
    ????echo?$exception->getMessage();
    }


    查看全部
  • /**
    ?*?方式一:ErrorException錯誤異常類
    ?*?@param?$errno
    ?*?@param?$errstr
    ?*?@param?$errfile
    ?*?@param?$errline
    ?*?@throws?ErrorException
    ?*/
    function?exception_error_handler($errno,$errstr,$errfile,$errline){
    
    ????throw?new?ErrorException($errstr,0,$errno,$errfile,$errline);
    }
    
    set_error_handler('exception_error_handler');
    
    try{
    ????echo?gettype();
    }catch?(Exception?$exception){
    ????echo?$exception->getMessage();
    }


    查看全部
  • /**
    ?*?自定義異常類處理器
    ?*?Class?ExceptionHandler
    ?*/
    
    class?ExceptionHandler
    {
    ?protected?$_exception;
    ?protected?$_logFile?=?__DIR__.'/exception_handle.log';
    ?public?function?__construct(Exception?$e)
    ????{
    ?$this->_exception?=?$e;
    ?}
    ?public?static?function?handle(Exception?$e)
    ????{
    ?$self?=?new?self($e);
    ?$self->log();
    ?echo?$self;
    ?}
    ?public?function?log()
    ????{
    ????????error_log($this->_exception->getMessage().PHP_EOL,3,$this->_logFile);
    ?}
    
    ?/**
    ?????*?魔術方法__toString()
    ?????*?快速獲取對象的字符串信息的便捷方式,直接輸出對象引用時自動調用的方法。
    ?*?@return?string
    ?????*/
    ?public?function?__toString()
    ????{
    ?$message?=?<<<EOF
    ????????<!DOCTYPE?html>
    ????????<html>
    ????????<head>
    ????????????<meta?charset="UTF-8">
    ????????????<title>Title</title>
    ????????</head>
    ????????<body>
    ????????????<h1>出現異常了啊啊啊啊</h1>
    ????????</body>
    ????????</html>
    EOF;
    ?return?$message;
    ?}
    
    }
    set_exception_handler(array('ExceptionHandler','handle'));
    /**
    ?*?try?catch不會被自定義異常處理!?。?!
    ?*/
    try{
    ?throw?new?Exception('this?is?a?test');
    }catch?(Exception?$exception)?{
    ?echo?$exception->getMessage();
    }
    throw?new?Exception('測試自定義的異常處理器');


    查看全部
  • /**
    ?*?自定義異常函數處理器
    ?*/
    header('content-type:text/html;charset=utf-8');
    function?exceptionHandler_1($e)
    {
    ????echo?'自定義異常處理器1<br/>函數名:'.__FUNCTION__.PHP_EOL;
    ????echo?'異常信息:'.$e->getMessage();
    }
    function?exceptionHandler_2($e)
    {
    ????echo?'自定義異常處理器2<br/>函數名:'.__FUNCTION__.PHP_EOL;
    ????echo?'異常信息:'.$e->getMessage();
    }
    
    set_exception_handler('exceptionHandler_1');
    //set_exception_handler('exceptionHandler_2');
    //恢復到上一次定義過的異常處理函數,即exceptionHandler_1
    //restore_exception_handler();
    //致命錯誤信息
    //restore_exception_handler();
    throw?new?Exception('測試自定義異常處理器');
    
    //自定義異常處理器,不會向下繼續執行;異常被捕獲之后,會繼續執行
    //回顧:自定義錯誤處理器會繼續執行代碼,而手動拋出的錯誤信息不會繼續執行
    echo?'test';


    查看全部
  • header('content-type:text/html;charset=utf-8');
    require_once?'Exception_Observer.php';
    require_once?'Logging_Exception_Observer.php';
    require_once?'Observable_Exception.php';
    
    Observable_Exception::attach(new?Logging_Exception_Observer());
    //Observable_Exception::attach(new?Logging_Exception_Observer('test.log'));
    
    class?MyException?extends?Observable_Exception{
    ????public?function?test1()
    ????{
    ????????echo?'this?is?a?test';
    ????}
    }
    
    try{
    ????throw?new?MyException('出現了異常!');
    }catch?(MyException?$exception){
    ????echo?$exception->getMessage();
    }


    查看全部
  • /**
    ?*?給觀察者定義規范
    ?*
    ?*?Interface?Exception_Observer
    ?*/
    interface?Exception_Observer
    {
    ????public?function?update(Observable_Exception?$e);
    }
    
    
    /**
    ?*?定義觀察者
    ?*?Class?Observable_Exception
    ?*/
    class?Observable_Exception?extends?Exception
    {
    ????//保存觀察者信息
    ????public?static?$_observers?=?array();
    ????public?static?function?attach(Exception_Observer?$observer)
    ????{
    ????????self::$_observers[]?=?$observer;
    ????}
    ????public?function?__construct($message?=?"",?$code?=?0,?Throwable?$previous?=?null)
    ????{
    ????????parent::__construct($message,?$code,?$previous);
    ????????$this->notify();
    ????}
    ????public?function?notify()
    ????{
    ????????foreach?(self::$_observers?as?$observer)?{
    ????????????$observer->update($this);
    ????????}
    ????}
    }
    
    /**
    ?*?記錄錯誤日志
    ?*/
    class?Logging_Exception_Observer?implements?Exception_Observer
    {
    ????protected?$_filename?=?__DIR__.'/error_observer.log';
    ????public?function?__construct($filename?=?null)
    ????{
    ????????if?($filename!==null?&&?is_string($filename)){
    ????????????$this->_filename?=?$filename;
    ????????}
    ????}
    
    ????public?function?update(Observable_Exception?$e)
    ????{
    ????????$message?=?"時間:".date('Y:m:d?H:i:s',time()).PHP_EOL;
    ????????$message.=?"信息:".$e->getMessage().PHP_EOL;
    ????????$message.=?"追蹤信息:".$e->getTraceAsString().PHP_EOL;
    ????????$message.=?"文件:".$e->getFile().PHP_EOL;
    ????????$message.=?"行號:".$e->getLine().PHP_EOL;
    ????????error_log($message,3,$this->_filename);//寫到日志中
    ????}
    }
    
    /**
    ?*?測試
    ?*/
    header('content-type:text/html;charset=utf-8');
    require_once?'Exception_Observer.php';
    require_once?'Logging_Exception_Observer.php';
    require_once?'Observable_Exception.php';
    
    Observable_Exception::attach(new?Logging_Exception_Observer());
    
    class?MyException?extends?Observable_Exception{
    ????public?function?test1()
    ????{
    ????????echo?'this?is?a?test';
    ????}
    }
    
    try{
    ????throw?new?MyException('出現了異常!');
    }catch?(MyException?$exception){
    ????echo?$exception->getMessage();
    }


    查看全部
  • try
    {
    ????//?需要進行異常處理的代碼段;
    ????throw?語句拋出異常;
    }catch(?Exception?$e?)
    {
    ????...?
    }
    catch(?Exception?$e?)
    {
    ????//?處理異常
    }
    contine.....
    
    //未被捕獲
    ????Fatal?error:Uncaught?exception.....

    什么時異常?異常和錯誤有什么區別?

    異常:程序運行與預期不太一致,與錯誤是兩個不同的概念


    查看全部
  • 1. 什么時異常?異常和錯誤有什么區別?

    2. 異常的基本語法結構

    3. 如何自定義異常類

    4. 如何自定義異常處理器

    5. 如何像處理異常一樣處理PHP的錯誤

    6. 在發生錯誤的時候將用戶重定向到另一個頁面


    查看全部

舉報

0/150
提交
取消
課程須知
學習本門課程之前,建議先了解一下知識,會更有助于理解和掌握本門課程 1、掌握PHP的基礎知識 2、了解面向對象編程
老師告訴你能學到什么?
1、PHP中的錯誤類型 2、PHP中的錯誤處理 3、PHP中自定義錯誤處理器 4、PHP中異常的使用 5、PHP中如何自定義異常類 6、PHP中如何自定義異常處理器

微信掃碼,參與3人拼團

微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

友情提示:

您好,此課程屬于遷移課程,您已購買該課程,無需重復購買,感謝您對慕課網的支持!