PHP Try catch Multi-level exception test

Source: Internet
Author: User
Tags php try catch try catch zend
 
  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 ';? >

The page in the try Catch uses C in the C1,C1 using B's b1,b1 to use a's A1.

The default is: A1 throws an exception, B1 captures the A1 exception, then throws the exception just now, C1 captures, then throws, the last page captures and outputs.
The result is:
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 (PNS): C->c1 ()
#3 C:\Program files\zend\zendstudio-5.2.0\bin\php5\dummy.php (1): Include (' D:\workspace\my ... ')
#4 {Main}end



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

End



A third Test:
throw new Exception ($e->getmessage ()) inside the B1; open.
Throws a new exception so that B1 calls above do not get A1 exceptions.
The result is:
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 (PNS): C->c1 ()
#2 C:\Program files\zend\zendstudio-5.2.0\bin\php5\dummy.php (1): Include (' D:\workspace\my ... ')
#3 {Main}end



Fourth Test:
Remove the try catch throw from the B1.
Results: still output The original exception, that is, the intermediate steps do not need to be thrown, the topmost can also get the lowest thrown exception.
There is only one problem, b if the first exception, there is no way to take, 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 (PNS): C->c1 ()
#3 C:\Program files\zend\zendstudio-5.2.0\bin\php5\dummy.php (1): Include (' D:\workspace\my ... ')
#4 {Main}end
  • Related Article

    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.