php異常處理機制

來源:互聯網
上載者:User

標籤:

•“@”符號可以告訴PHP抑制所有由其後面的本行代碼內容產生的錯誤。  

<?php

try

{

    throw new exception(‘發生錯誤!‘, 42);

}

catch (exception $e)

{

       echo ‘編號 ‘ .$e->getCode().‘ :‘ .$e->getMessage();

}

?> 

PHP 5中提供了exception異常類,該類除了建構函式外,還提供了一些內建方法:

•getCode():返回傳遞給建構函式的代碼•getMessage():返回傳遞給建構函式的訊息•getFile():返回產生異常的代碼檔案的完整路徑•getLine():傳回碼檔案中產生異常的程式碼號•getTrace():返回一個包含了產生異常的代碼回退路徑的數組•getTraceAsString():返回與getTrace()方向相同的資訊,該資訊將被格式化成字串。•_toString():允許簡單地顯示一個exception對象,並且給出以上所有方法可以提供的資訊。 

<?php

class MyException extends exception

{

function _toString()

{

return ‘<table><tr><td>Exception ‘ .$this->getCode().

‘ :‘ .$this->getMessage(). ‘<br />in ‘ .$this->getFile().

‘ on line ‘ .$this->getLine(). ‘</td></tr></table>‘;

}

}

try

{

throw new MyException(‘出錯啦!‘, 42);

}

catch(MyException $e)

{

echo $e; // 自動調用 _toString() 方法。

}

?> 

php異常處理機制

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.