php中error與exception的區別及應用

來源:互聯網
上載者:User

   這篇文章主要介紹了php中error與exception的區別及應用,需要的朋友可以參考下

  error與exception的區別查閱網上資料大多是java的解釋,貌似php的異常處理過程與java差不多

  java中的Object繼承結構如下:

  Object---->Throwable--------> Exception ----> RuntimeException | Error

  Error都是unchecked類型 Exception分為checked 與 unchecked類型

  且把異常與錯誤都當作程式運行不正常的現象來看待

  如果區分異常與錯誤的話:

  異常:非致命的。try{}catche(Exception e){} 執行中的try模組是測試回合,代碼在運行過程中出現錯誤(非致命錯誤)執行catche

  異常的作用類似於下面的這些代碼:

  if(mysql_connect('127.0.0.1','root','321321'))

  {

  echo '串連資料庫成功';

  // other code...

  }

  else

  {

  echo '串連資料庫錯誤';

  return false;

  }

  使用異常處理的情況可以很輕鬆的處理異常情況。如下面的代碼可以一次性處理很多的異常情況

  try

  {

  mysql_connect('127.0.0.1','root','321321');

  // other code you want to execute

  }catche(Exception $e){

  print_r($e);

  }

聯繫我們

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