xref: /php-src/Zend/tests/throw/001.phpt (revision fbe30592)
1--TEST--
2throw expression
3--FILE--
4<?php
5
6try {
7    $result = true && throw new Exception("true && throw");
8    var_dump($result);
9} catch (Exception $e) {
10    var_dump($e->getMessage());
11}
12
13try {
14    $result = false && throw new Exception("false && throw");
15    var_dump($result);
16} catch (Exception $e) {
17    var_dump($e->getMessage());
18}
19
20try {
21    $result = true and throw new Exception("true and throw");
22    var_dump($result);
23} catch (Exception $e) {
24    var_dump($e->getMessage());
25}
26
27try {
28    $result = false and throw new Exception("false and throw");
29    var_dump($result);
30} catch (Exception $e) {
31    var_dump($e->getMessage());
32}
33
34try {
35    $result = true || throw new Exception("true || throw");
36    var_dump($result);
37} catch (Exception $e) {
38    var_dump($e->getMessage());
39}
40
41try {
42    $result = false || throw new Exception("false || throw");
43    var_dump($result);
44} catch (Exception $e) {
45    var_dump($e->getMessage());
46}
47
48try {
49    $result = true or throw new Exception("true or throw");
50    var_dump($result);
51} catch (Exception $e) {
52    var_dump($e->getMessage());
53}
54
55try {
56    $result = false or throw new Exception("false or throw");
57    var_dump($result);
58} catch (Exception $e) {
59    var_dump($e->getMessage());
60}
61
62try {
63    $result = null ?? throw new Exception("null ?? throw");
64    var_dump($result);
65} catch (Exception $e) {
66    var_dump($e->getMessage());
67}
68
69try {
70    $result = "foo" ?? throw new Exception('"foo" ?? throw');
71    var_dump($result);
72} catch (Exception $e) {
73    var_dump($e->getMessage());
74}
75
76try {
77    $result = null ?: throw new Exception("null ?: throw");
78    var_dump($result);
79} catch (Exception $e) {
80    var_dump($e->getMessage());
81}
82
83try {
84    $result = "foo" ?: throw new Exception('"foo" ?: throw');
85    var_dump($result);
86} catch (Exception $e) {
87    var_dump($e->getMessage());
88}
89
90try {
91    $callable = fn() => throw new Exception("fn() => throw");
92    var_dump("not yet");
93    $callable();
94} catch (Exception $e) {
95    var_dump($e->getMessage());
96}
97
98$result = "bar";
99try {
100    $result = throw new Exception();
101} catch (Exception $e) {}
102var_dump($result);
103
104try {
105    var_dump(
106        throw new Exception("exception 1"),
107        throw new Exception("exception 2")
108    );
109} catch (Exception $e) {
110    var_dump($e->getMessage());
111}
112
113try {
114    $result = true ? true : throw new Exception("true ? true : throw");
115    var_dump($result);
116} catch (Exception $e) {
117    var_dump($e->getMessage());
118}
119
120try {
121    $result = false ? true : throw new Exception("false ? true : throw");
122    var_dump($result);
123} catch (Exception $e) {
124    var_dump($e->getMessage());
125}
126
127try {
128    throw new Exception() + 1;
129} catch (Throwable $e) {
130    var_dump($e->getMessage());
131}
132
133try {
134    throw $exception = new Exception('throw $exception = new Exception();');
135} catch (Exception $e) {}
136var_dump($exception->getMessage());
137
138try {
139    $exception = null;
140    throw $exception ??= new Exception('throw $exception ??= new Exception();');
141} catch (Exception $e) {}
142var_dump($exception->getMessage());
143
144try {
145    throw null ?? new Exception('throw null ?? new Exception();');
146} catch (Exception $e) {
147    var_dump($e->getMessage());
148}
149
150?>
151--EXPECT--
152string(13) "true && throw"
153bool(false)
154string(14) "true and throw"
155bool(false)
156bool(true)
157string(14) "false || throw"
158bool(true)
159string(14) "false or throw"
160string(13) "null ?? throw"
161string(3) "foo"
162string(13) "null ?: throw"
163string(3) "foo"
164string(7) "not yet"
165string(13) "fn() => throw"
166string(3) "bar"
167string(11) "exception 1"
168bool(true)
169string(20) "false ? true : throw"
170string(42) "Unsupported operand types: Exception + int"
171string(35) "throw $exception = new Exception();"
172string(37) "throw $exception ??= new Exception();"
173string(30) "throw null ?? new Exception();"
174