PhpTryCatch exception test_php tutorial

Source: Internet
Author: User
PhpTryCatch exception test. Use c1 of c in trycatch, 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 uses c1 of c in try catch on the page, use 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:Everything is normal, 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

Class {
Public function a1 (){
Try {
Throw new Exception ('20140901 ');
} Catch (Exception $ e ){
Throw $ e;
}
}
}

Class B {
Public function b1 (){
Try {
$ A = new ();
$ 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;

?>

Use c1 of c in catch, 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 ,...

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.