PHP try catch exception Test

Source: Internet
Author: User
Tags php try catch

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: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

<? PHP
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;

?>

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.