php5程式異常處理_PHP教程

來源:互聯網
上載者:User
異常的工作原理:
try
{
代碼處理常式;
if(代碼處理髮生錯誤)throw new Exception('拋出一個異常');//使用throw關鍵字,後面是Exception的一個對象
//需要說明的是php5異常不會自動拋出異常
//拋出異常後下面處理常式不再執行
代碼處理常式;
}
catch Exception $e
{
處理異常;
//如:echo 'Exception '.$e->getCode().':'.$e->getMessage().'in'.$e->getFile().'on line'.$e->getLine();
}

看看拋出的異常類系統是如何定義的
class Exception
{
protected $message='Unknown exception';
protected $code=0;
protected $file;
protected $line;

function __construct($message=null,$code=0);

final function getMessage();
final function getCode();
final function getFile();
final function getLine();
final function getTrace();
final function getTraceAsString();//與getTrace()一樣,只不過它將格式化為字串

function __toString();//需要對象的字串表示時會自動調用這個方法,也就是一旦有echo或print直接輸出Exception執行個體時就會被調用
}

Exception的屬性不能在調用代碼中直接存取,而必須使用擷取方法獲得其屬性值,只用$message,$code能有使用者拋出異常時設定,即給Exception類的建構函式完成。由Exception類可以看出我們完全可以繼承Exception類建立我們自己的異常類,在檢測錯誤時不再拋出系統預設的異常對象,而是我們自訂的異常類對象,但我們只能繼承建構函式、toString()方法和Exception的屬性。

class myException extends Exception
{
function __toString()
{
return 'Exception '.$this->getCode().':'.$this->getMessage().'in'.$this->getFile().'on line'.$this->getLine().'';//改寫拋出異常結果
}
}

在代碼中調用我們自訂的異常類
try
{
throw new myException('錯誤',10);//為了簡單,直接拋出自訂異常對象
}
catch(myException $e)
{
echo $e;//由於我們的自訂異常已經改變了輸出方式,所以這裡直接輸入異常對象即可,原因為一旦有echo或print直接輸出Exception執行個體時就會被調用__toString()
}

以上單間的介紹了異常,下面我們把異常引入我們的資料庫處理類中看看如何使用
class myException extends Exception{
function __construct($message=null,$code=0)
{
parent::__construct($message,$code);
}
function __toString()
{
return 'Exception '.$this->getCode().':'.$this->getMessage().'in File:'.$this->getFile().' on line:'.$this->getLine().'';//改寫拋出異常結果
}
}
class mydb {
private $user;//使用者名稱
private $pass;//密碼
private $host;//資料庫ip地址或localhost
private $db; //資料庫名

//建構函式
public function __construct (){
$num_args = func_num_args();

if($num_args > 0){
$args = func_get_args();
$this->host = $args[0];
$this->user = $args[1];
$this->pass = $args[2];
//傳入參數後自動調用資料庫連接
$this->connect();
}
}

//資料庫連接
private function connect (){
//異常處理
try {
if (!$this->db =@ mysql_connect ($this->host,$this->user,$this->pass)){
$exceptionstring = "串連失敗:主機、使用者名稱或密碼錯誤 ";
throw new myException ($exceptionstring,0);//拋出自訂異常對象,執行個體對象時參數錯誤
}
} catch (myException $e) {
echo $e;
}
}

//資料庫選擇
public function selectdb ($thedb){
try {
if ([email=!@mysql_select_db]!@mysql_select_db[/email] ($thedb, $this->db)){
$exceptionstring = "資料庫: $thedb不存在 ";
throw new myException ($exceptionstring,1);
}
} catch (myException $e) {
echo $e;
}
}

//執行一個update,delete
public function execute ($thequery){
try {
if ([email=!@mysql_query]!@mysql_query[/email] ($thequery, $this->db)){
$exceptionstring = "錯誤的sql語句: $thequery ";
throw new myException ($exceptionstring,2);
} else {
echo "update/delete受影響: " . mysql_affected_rows () . " 行
";
}
} catch (myException $e) {
echo $e;
}
}

//返回插敘結果
public function getrows ($thequery){
try {
if (!$aquery =@ mysql_query ($thequery)){
$exceptionstring = "錯誤的查詢語句: $thequery ";
throw new myException ($exceptionstring,3);
} else {
while ($adata = mysql_fetch_array ($aquery)){
$returnarr[] =$adata;
}
return $returnarr;
}
} catch (myException $e) {
echo $e;
}
}

//解構函式關閉串連
public function __destruct() {
try {
if ([email=!@mysql_close]!@mysql_close[/email] ($this->db)){
$exceptionstring = "關閉串連失敗 ";
throw new myException ($exceptionstring,4);
}
} catch (myException $e) {
echo $e;
}
}

}
//執行個體類
$mydb = new mydb ("localhost","root","123456");
//選擇資料庫
$mydb->selectdb ("tonlong");
//執行更新操作
$adata = $mydb->execute ("update db_news set hits=hits+1 where id=3");
//查詢輸出
$data = $mydb->getrows ("select title from db_news");
for ($i = 0; $i < count ($data); $i++){
echo $data[$i][0] ."
";
}
?>

http://www.bkjia.com/PHPjc/629750.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/629750.htmlTechArticle異常的工作原理: try { 代碼處理常式; if(代碼處理髮生錯誤)throw new Exception('拋出一個異常');//使用throw關鍵字,後面是Exception的一個對象 //...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.