2 // Copyright 2004-2015 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()');
28 public function __destruct() {
29 printf("In ExitDestruct::__destruct()\n");
35 $td2 = new ThrowDestruct2
;
37 // The exit() in ExitDestruct::__destruct() should prevent the destructor
38 // for $td2 from running, as well as any up-stack destructors.
39 $ed = new ExitDestruct
;
43 $td1 = new ThrowDestruct1
;
44 printf("Calling bar()\n");
46 printf("After bar()\n");
49 function printPreviousExceptions($e) {
52 while ($e = $e->getPrevious()) {
53 printf("\tPrevious #%d: %s\n", ++
$i, $e->getMessage());
58 printf("main() starting\n");
61 printf("Calling foo()\n");
63 printf("After foo()\n");
65 catch (Exception
$e) {
66 printf("Caught %s in main()\n", $e->getMessage());
67 printPreviousExceptions($e);
70 printf("main() ending\n");
73 printf("Calling main()\n");
75 printf("Returned from main(), exiting\n");