Happy New Year (again)
[hiphop-php.git] / hphp / test / quick / exception_destructor_4.php
blob98dbc80b7392715c0fbea2a814762027a6aafc92
1 <?php
2 // Copyright 2004-2014 Facebook. All Rights Reserved.
4 // Test behavior around exceptions leaking out of destructors.
5 // Specifically, fatals vs. user exceptions.
7 class Ex1 extends Exception {
10 class Ex2 extends Exception {
13 class ThrowDestruct1 {
14 public function __destruct() {
15 printf("In ThrowDestruct1::__destruct()\n");
16 throw new Ex1('Exception leaked out of ThrowDestruct1::__destruct()');
20 class ThrowDestruct2 {
21 public function __destruct() {
22 printf("In ThrowDestruct2::__destruct()\n");
23 throw new Ex2('Exception leaked out of ThrowDestruct2::__destruct()');
27 class ExitDestruct {
28 public function __destruct() {
29 printf("In ExitDestruct::__destruct()\n");
30 exit();
34 function bar() {
35 $td2 = new ThrowDestruct2;
36 // The exit() in ExitDestruct::__destruct() should prevent the destructor
37 // for $td2 from running, as well as any up-stack destructors.
38 $ed = new ExitDestruct;
39 printf("Throwing in bar()\n");
40 throw new Exception('Exception from bar()');
43 function foo() {
44 $td1 = new ThrowDestruct1;
45 printf("Calling bar()\n");
46 bar();
47 printf("After bar()\n");
50 function printPreviousExceptions($e) {
51 $i = 0;
53 while ($e = $e->getPrevious()) {
54 printf("\tPrevious #%d: %s\n", ++$i, $e->getMessage());
58 function main() {
59 printf("main() starting\n");
61 try {
62 printf("Calling foo()\n");
63 foo();
64 printf("After foo()\n");
66 catch (Exception $e) {
67 printf("Caught %s in main()\n", $e->getMessage());
68 printPreviousExceptions($e);
71 printf("main() ending\n");
74 printf("Calling main()\n");
75 main();
76 printf("Returned from main(), exiting\n");