《PHP應(yīng)用:PHP異常處理淺析》要點(diǎn):
本文介紹了PHP應(yīng)用:PHP異常處理淺析,希望對您有用。如果有疑問,可以聯(lián)系我們。
PHP實(shí)例PHP預(yù)定了兩個(gè)非常類:Exception和ErrorException
代碼如下:
Exception {
??? /* 屬性 */
??? protected string $message ; //異常消息內(nèi)容
??? protected int $code ; //異常代碼號(hào)
??? protected string $file ; //拋出異常的文件名
??? protected int $line ; //拋出異常在該文件中的行號(hào)
??? /* 辦法 */
??? public __construct ([ string $message = "" [, int $code = 0 [, Exception $previous = null]]] )
??? final public string getMessage ( void ) //異常拋出的信息
??? final public Exception getPrevious ( void ) //前一異常
??? final public int getCode ( void ) //異常代碼,這是用戶自定義的
??? final public string getFile ( void ) //發(fā)生異常的文件路勁
??? final public int getLine ( void ) //發(fā)生異常的行
??? final public array getTrace ( void ) //異常追蹤信息(array)
??? final public string getTraceAsString ( void ) //異常追蹤信息(string)
??? public string __toString ( void ) //試圖直接 將異常對象當(dāng)作字符串使用時(shí)調(diào)用子函數(shù)的返回值
??? final private void __clone ( void ) //克隆異常對象時(shí)調(diào)用
}
代碼如下:
ErrorException? extends Exception? {
?
??? /* 屬性 */
??? protected int $severity?? ;
??? /* 辦法 */
???
??? public __construct? ([ string $message? = ""? [, int $code? = 0? [, int $severity? = 1? [, string $filename? = __FILE__? [, int $lineno? = __LINE__? [, Exception? $previous? = NULL??? ]]]]]] )
??? final public int getSeverity? ( void )
??? /* 繼承的辦法 */
??? final public string Exception::getMessage? ( void )
??? final public Exception Exception::getPrevious? ( void )
??? final public int Exception::getCode? ( void )
??? final public string Exception::getFile? ( void )
??? final public int Exception::getLine? ( void )
??? final public array Exception::getTrace? ( void )
??? final public string Exception::getTraceAsString? ( void )
??? public string Exception::__toString? ( void )
??? final private void Exception::__clone? ( void )
}
PHP實(shí)例那么如何捕獲異常?
PHP實(shí)例(1)PHP可用try...catch...捕獲異常,進(jìn)行異常處理的代碼必需在try代碼塊內(nèi).
代碼如下:
try {
??? throw new Exception('exception test 1', 1001);
} catch(Exception $e) {
??? echo $e->getMessage().'-'.$e->getCode();
}
PHP實(shí)例(2)用戶可以自定義異常處理函數(shù)[set_exception_handler],用于沒用用try/catch捕捉的異常.
代碼如下:
function? exception_handler ( $e ) {
??? echo? "Uncaught exception: "? ,? $e -> getMessage (),? "\n" ;
}
?
set_exception_handler ( 'exception_handler' );
?
throw new? Exception ( 'Uncaught Exception' );
?
echo "這行不會(huì)執(zhí)行了";
PHP實(shí)例可以看到使用ser_exception_handler回調(diào)函數(shù)處理異常,后續(xù)的代碼不會(huì)繼續(xù)執(zhí)行,但try-catch可以.
(3)PHP可用多catch捕捉不同類型異常,并允許在catch代碼塊內(nèi)再次拋出異常.
代碼如下:
//請根據(jù)實(shí)際擴(kuò)展非常類
class MyException extends Exception {
??? public function __construct($message = '', $code = 0) {
?
??? }
?
??? public function myFunction() {
??????? echo 'just for test';
??? }
}
?
try {
??? throw new MyException('an error');
} catch (MyException $e) {
??? echo $e->myFunction();
} catch (Exception $e) {
??? echo $e->getMessage();
}
(4)PHP5.5已經(jīng)支持finally關(guān)鍵詞,你無需關(guān)心非常是否溢出了.
PHP實(shí)例![PHP應(yīng)用:PHP異常處理淺析]()
PHP實(shí)例可對好比下:
代碼以下:
function doSomething() {
??? $resource = createResource();
??? try {
??????? $result = useResource($resource);
??? } catch (Exception $e) {
??????? releaseResource($resource);
??????? log($e->getMessage());
??????? exit();
??? }
??? releaseResource($resource);
??? return $result;
}
?
//使用finally后
function doSomething2() {
??? $resource = createResource();
??? try {
??????? $result = useResource($resource);
??????? return $result;
??? } catch (Exception $e) {
??????? log($e->getMessage());
??????? exit();
??? } finally {
??????? releaseResource($resource);
??? }
}
《PHP應(yīng)用:PHP異常處理淺析》是否對您有啟發(fā),歡迎查看更多與《PHP應(yīng)用:PHP異常處理淺析》相關(guān)教程,學(xué)精學(xué)透。維易PHP學(xué)院為您提供精彩教程。
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/10879.html