xref: /PHP-8.3/Zend/tests/throw/002.phpt (revision 7aacc705)
1--TEST--
2Test throw with various expressions
3--FILE--
4<?php
5
6class Foo {
7    public function createNotFoundException() {
8        return new Exception('Not found');
9    }
10
11    public function throwException() {
12        throw $this->createNotFoundException();
13    }
14
15    public static function staticCreateNotFoundException() {
16        return new Exception('Static not found');
17    }
18
19    public static function staticThrowException() {
20        throw static::staticCreateNotFoundException();
21    }
22}
23
24try {
25    (new Foo())->throwException();
26} catch(Exception $e) {
27    echo $e->getMessage() . "\n";
28}
29
30try {
31    Foo::staticThrowException();
32} catch(Exception $e) {
33    echo $e->getMessage() . "\n";
34}
35
36try {
37    throw true ? new Exception('Ternary true 1') : new Exception('Ternary true 2');
38} catch(Exception $e) {
39    echo $e->getMessage() . "\n";
40}
41
42try {
43    throw false ? new Exception('Ternary false 1') : new Exception('Ternary false 2');
44} catch(Exception $e) {
45    echo $e->getMessage() . "\n";
46}
47
48try {
49    $exception1 = new Exception('Coalesce non-null 1');
50    $exception2 = new Exception('Coalesce non-null 2');
51    throw $exception1 ?? $exception2;
52} catch(Exception $e) {
53    echo $e->getMessage() . "\n";
54}
55
56try {
57    $exception1 = null;
58    $exception2 = new Exception('Coalesce null 2');
59    throw $exception1 ?? $exception2;
60} catch(Exception $e) {
61    echo $e->getMessage() . "\n";
62}
63
64try {
65    throw $exception = new Exception('Assignment');
66} catch(Exception $e) {
67    echo $e->getMessage() . "\n";
68}
69
70try {
71    $exception = null;
72    throw $exception ??= new Exception('Coalesce assignment null');
73} catch(Exception $e) {
74    echo $e->getMessage() . "\n";
75}
76
77try {
78    $exception = new Exception('Coalesce assignment non-null 1');
79    throw $exception ??= new Exception('Coalesce assignment non-null 2');
80} catch(Exception $e) {
81    echo $e->getMessage() . "\n";
82}
83
84$andConditionalTest = function ($condition1, $condition2) {
85    throw $condition1 && $condition2
86        ? new Exception('And in conditional 1')
87        : new Exception('And in conditional 2');
88};
89
90try {
91    $andConditionalTest(false, false);
92} catch(Exception $e) {
93    echo $e->getMessage() . "\n";
94}
95
96try {
97    $andConditionalTest(false, true);
98} catch (Exception $e) {
99    echo $e->getMessage() . "\n";
100}
101
102try {
103    $andConditionalTest(true, false);
104} catch (Exception $e) {
105    echo $e->getMessage() . "\n";
106}
107
108try {
109    $andConditionalTest(true, true);
110} catch (Exception $e) {
111    echo $e->getMessage() . "\n";
112}
113
114?>
115--EXPECT--
116Not found
117Static not found
118Ternary true 1
119Ternary false 2
120Coalesce non-null 1
121Coalesce null 2
122Assignment
123Coalesce assignment null
124Coalesce assignment non-null 1
125And in conditional 2
126And in conditional 2
127And in conditional 2
128And in conditional 1
129