PhpTryCatch multi-level error test

Source: Internet
Author: User
Tags php try catch
PhpTryCatch multi-level exception test & lt ;? Phpclassa {publicfunctiona1 () {try {thrownewException (123);} catch (Exception $ e) {throw $ e ;}} classb {publicfunctionb1 () {t php Try Catch multi-level exception test
 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';?>

Use c1 of c in try catch on the page, b1 of B in c1, and a1 of a in b1.

The default value is: a1 throws an exception, b1 captures the a1 exception, and then throws the exception, c1 captures the exception, and then throws the exception. Finally, the page captures and outputs the exception.
The result is:
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



Second Test:
Remove throw $ e from b1, that is, do not throw it.
The result is:
X-Powered-By: PHP/5.1.1
Content-type: text/html

End



Third Test:
Open throw new Exception ($ e-> getMessage () in b1.
Throw a new exception, so that none of the calls above b1 can get the a1 exception.
The result is:
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



Fourth Test:
Remove all try catch throw in b1.
Result: The original exception is still output, that is, the middle step does not need to be thrown, and the top layer can also get the Exception thrown at the bottom layer.
There is only one problem. if an exception occurs in B, it cannot be obtained. if you want to detect B, add try catch in B.

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

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.