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()');
28 public function __destruct() {
29 printf("In ExitDestruct::__destruct()\n");
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()');
44 $td1 = new ThrowDestruct1
;
45 printf("Calling bar()\n");
47 printf("After bar()\n");
50 function printPreviousExceptions($e) {
53 while ($e = $e->getPrevious()) {
54 printf("\tPrevious #%d: %s\n", ++
$i, $e->getMessage());
59 printf("main() starting\n");
62 printf("Calling foo()\n");
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");
76 printf("Returned from main(), exiting\n");