看完了out_put_fns.php檔案,讓我們再看看db_fns.php檔案。其代碼非常簡單,如下:
1 2
3 function db_connect()
4 {
5 $result = new mysqli( ' localhost ' , ' bm_user ' , ' password ' , ' bookmarks ' );
6 if ( ! $result )
7 throw new Exception ( ' Could not connect to database server ' );
8 else
9 return $result ;
10 }
11
12 ?>
其作用是串連資料庫,並返回一個資料庫連接。在這裡我們暫且不提資料庫連接,因為第7行的代碼是拋出一個異常的。所以我們先討論PHP的異常,然後在下一章節中專門講解資料庫的操作等。
PHP的異常機制和Java等語言差不多。但是還是有區別的。
PHP的異常同樣是以try...throw...catch來捕獲異常。
在某些語言,例如C#,Java,try裡的代碼有的時候會自動拋出異常,但是載PHP中,你必須手動捕獲這個異常。和其他語言一樣,PHP也會判斷合適的異常拋出,那就是後面catch的作用了。
PHO也有異常的類。讓我們先看一個例子,代碼如下:
1 2 try
3 {
4 throw new Exception('An Exception occurs here!',43);
5 }
6 catch(Exception $e)
7 {
8 echo 'Exception '.$e->getCode().':'.$e->getMessage().'in'.$e->getFile().'on line'
9 .$e->getLine().'
';
10
11 }
12 ?>
它將輸出:
Exception43 : An Exception occurs here ! inG : \Apache Group\Apache2 . 2 \htdocs\test . phpon line4
這裡我們看到了Exception類的使用。如果你對C#和Java熟悉的話,相信不是很難看懂。
PHP5提供了Exception類,其構造時需要2個參數,一個是異常訊息,一個是異常代碼。
除了建構函式之外,它還包括以下函數。
getCode()--返回傳遞給建構函式的代碼。 getMessage()--返回給建構函式的訊息。 getFile()--返回產生異常的代碼的檔案的完整路徑。 getLine()--返回產生異常代碼的行號。 getTrace()--返回一個產生異常的代碼以及回退路徑,這個和.net裡的異常,當你編寫一個ASP.NET頁面時,如果發生異常,.net會將錯誤的資訊,所在的檔案,以及回退路徑資訊全部提供給你。
getTraceAsString()--與getTrace()一樣,只不過它將格式化為字串。 __toString()--允許簡單的顯示Exception對象,並且給出所有以上方法給出的資訊。 可以調用 echo $e顯示所有資訊。例如上面的代碼如此調用,結果是
exception ' Exception ' with message ' An Exception occurs here! ' in G : \Apache Group\Apache2 . 2 \htdocs\test . php : 4 Stack trace : # 0 {main}
和其他語言一樣,PHP也可以自訂Exception類。
幸運的是PHP提供了Exception的代碼。讓我們一睹為快。
1 2 class Exception
3 {
4 protected $message = ' Unknown exception ' ; // exception message
5 protected $code = 0 ; // user defined exception code
6 protected $file ; // source filename of exception
7 protected $line ; // source line of exception
8
9 function __construct( $message = null , $code = 0 );
10
11 final function getMessage(); // message of exception
12 final function getCode(); // code of exception
13 final function getFile(); // source filename
14 final function getLine(); // source line
15 final function getTrace(); // an array of the backtrace()
16 final function getTraceAsString(); // formated string of trace
17
18 /* Overrideable */
19 function __toString(); // formated string for display
20 }
21 ?>
讓我們看看這個類,如果我們打算自訂自己的異常,必須從繼承這個類。看樣子只有__toString可以重寫,因為其他的方法都有final關鍵字,說明子類是沒法重寫的。看看下面的例子吧:
1 2 try
3 {
4 throw new user_defined_exception( ' An Exception occurs here! ' , 43 );
5 }
6 catch (user_defined_exception $e )
7 {
8 echo $e ;
9 // echo 'Exception '.$e->getCode().':'.$e->getMessage().'in'.$e->getFile().'on line'
10 //.$e->getLine().'
';
11
12 }
13 class user_defined_exception extends Exception
14 {
15 public function __toString()
16 {
17 return '
18 Exception ' . $this -> getCode() . ' : ' . $this -> getMessage() . ' in ' . $this -> getFile() . ' on line ' 19 . $this -> getLine() . ' |
' ;
20 }
21 }
22 ?>
輸出為:
| Exception 43:An Exception occurs here!inG:\Apache Group\Apache2.2\htdocs\test.phpon line4 |
總結,本章討論了PHP異常的特點,與其他語言的一些不同之處。最後還介紹了自訂的異常。