PhpTryCatch 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