PHP異常處理淺析

來源:互聯網
上載者:User

   這篇文章主要介紹了PHP異常處理淺析,本文著重講解如何捕獲異常,並給出代碼操作執行個體,需要的朋友可以參考下

  PHP預定了兩個異常類:Exception和ErrorException

   代碼如下:

  Exception {

  /* 屬性 */

  protected string $message ; //異常訊息內容

  protected int $code ; //異常代碼號

  protected string $file ; //拋出異常的檔案名稱

  protected int $line ; //拋出異常在該檔案中的行號

  /* 方法 */

  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 ) //發生異常的檔案路勁

  final public int getLine ( void ) //發生異常的行

  final public array getTrace ( void ) //異常追蹤資訊(array)

  final public string getTraceAsString ( void ) //異常追蹤資訊(string)

  public string __toString ( void ) //試圖直接 將異常對象當作字串使用時調用子函數的傳回值

  final private void __clone ( void ) //複製異常對象時調用

  }

   代碼如下:

  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 )

  }

  那麼如何捕獲異常?

  (1)PHP可用try...catch...捕獲異常,進行異常處理的代碼必須在try代碼塊內。

   代碼如下:

  try {

  throw new Exception('exception test 1', 1001);

  } catch(Exception $e) {

  echo $e->getMessage().'-'.$e->getCode();

  }

  (2)使用者可以自訂異常處理函數[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 "這行不會執行了";

  可以看到使用ser_exception_handler回呼函數處理異常,後續的代碼不會繼續執行,但try-catch可以。

  (3)PHP可用多catch捕獲不同類型異常,並允許在catch代碼塊內再次拋出異常。

  代碼如下:

  //請根據實際擴充異常類

  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已經支援finally關鍵詞,你無需關心異常是否溢出了。

  可對比如下:

   代碼如下:

  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);

  }

  }

相關文章

聯繫我們

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