php Try Catch多層級異常測試

來源:互聯網
上載者:User
a1();  } catch (Exception $e) {   throw $e;   //throw new Exception($e->getMessage());  } }}class c { public function c1 () {  try {   $a = new b();   $a->b1();  } catch (Exception $e) {   throw $e;  } }}try { $c = new c();  $c->c1();} catch (Exception $e) { echo $e->getTraceAsString();}echo 'end';?>

頁面try catch裡使用c的 c1,c1裡使用b的b1,b1裡使用a的a1。

預設的是:a1裡拋出異常,b1裡捕獲a1的異常,然後再把剛才的異常拋出,c1捕獲,然後拋出,最後頁面捕獲並輸出。
結果是:
X-Powered-By: PHP/5.1.1
Content-type: text/html

#0 D:\workspace\myzCollection\test.php(16): a->a1()
#1 D:\workspace\myzCollection\test.php(28): b->b1()
#2 D:\workspace\myzCollection\test.php(37): c->c1()
#3 C:\Program Files\Zend\ZendStudio-5.2.0\bin\php5\dummy.php(1): include('D:\workspace\my...')
#4 {main}end



第二個測試:
把b1裡面的throw $e去掉,就是不拋出。
結果是:
X-Powered-By: PHP/5.1.1
Content-type: text/html

end



第三個測試:
把b1裡面的throw new Exception($e->getMessage());開啟。
拋出一個新的異常,這樣b1以上的調用都拿不到a1的異常了。
結果是:
X-Powered-By: PHP/5.1.1
Content-type: text/html

#0 D:\workspace\myzCollection\test.php(28): b->b1()
#1 D:\workspace\myzCollection\test.php(37): c->c1()
#2 C:\Program Files\Zend\ZendStudio-5.2.0\bin\php5\dummy.php(1): include('D:\workspace\my...')
#3 {main}end



第四個測試:
把b1裡面的try catch throw都去掉。
結果: 仍然輸出原來的異常,就是說中間的步驟不需要拋出,最上層也能拿到最下層拋出的異常。
只是有一個問題,b中如果出先異常,就沒有辦法取到,如果需要也檢測b的話,那麼也要在b中加上try catch

X-Powered-By: PHP/5.1.1
Content-type: text/html

#0 D:\workspace\myzCollection\test.php(16): a->a1()
#1 D:\workspace\myzCollection\test.php(28): b->b1()
#2 D:\workspace\myzCollection\test.php(37): c->c1()
#3 C:\Program Files\Zend\ZendStudio-5.2.0\bin\php5\dummy.php(1): include('D:\workspace\my...')
#4 {main}end
  • 聯繫我們

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