PHP Try catch exception test _php tips

Source: Internet
Author: User
Tags getmessage php try catch try catch zend

In a page try catch, use the A1 of a in the c1,c1 of C using the B1,B1 in B.

The default is: A1 throws an exception, B1 catch the A1 exception, and then throw the exception, C1 capture, and then throw, the last page capture and output.
The result:
x-powered-by:php/5.1.1
Content-type:text/html

#0 D:\workspace\myzCollection\test.php (): A->a1 ()
#1 D:\workspace\myzCollection\test.php (): B->b1 ()
#2 D:\workspace\myzCollection\test.php (Panax): C->c1 ()
#3 C:\Program files\zend\zendstudio-5.2.0\bin\php5\dummy.php (1): Include (' D:\workspace\my ... ')
#4 {Main}end

Second Test:
B1 inside the throw $e removed, is not thrown.
The result:
x-powered-by:php/5.1.1
Content-type:text/html

End

A third Test:
Put the B1 inside the throw new Exception ($e->getmessage ());
Throws a new exception so that the A1 exception is not b1 to the call above.
The result:
x-powered-by:php/5.1.1
Content-type:text/html

#0 D:\workspace\myzCollection\test.php (): B->b1 ()
#1 D:\workspace\myzCollection\test.php (Panax): C->c1 ()
#2 C:\Program files\zend\zendstudio-5.2.0\bin\php5\dummy.php (1): Include (' D:\workspace\my ... ')
#3 {Main}end

A fourth Test:
Remove the try catch throw in the B1.
Result: Everything is normal, that is, the Middle step does not need to throw, the top can also get the lowest level of the exception thrown.
There is only one problem, b if the first exception, there is no way to, if you need to also detect B, then also in B to add a try Catch
x-powered-by:php/5.1.1
Content-type:text/html

#0 D:\workspace\myzCollection\test.php (): A->a1 ()
#1 D:\workspace\myzCollection\test.php (): B->b1 ()
#2 D:\workspace\myzCollection\test.php (Panax): C->c1 ()
#3 C:\Program files\zend\zendstudio-5.2.0\bin\php5\dummy.php (1): Include (' D:\workspace\my ... ')
#4 {Main}end

<?php
Class A {public
 function A1 () {
 try {
  throw new Exception (' 123 ');
 \ catch (Exception $e) { C6/>throw $e;
 }

}} Class B {public
 function B1 () {
 try {
  $a = new A ();
  $a->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 789;

? >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.