|
| 1 | +--TEST-- |
| 2 | +throw expression |
| 3 | +--FILE-- |
| 4 | +<?php |
| 5 | + |
| 6 | +try { |
| 7 | + $result = true && throw new Exception("true && throw"); |
| 8 | + var_dump($result); |
| 9 | +} catch (Exception $e) { |
| 10 | + var_dump($e->getMessage()); |
| 11 | +} |
| 12 | + |
| 13 | +try { |
| 14 | + $result = true || throw new Exception("true || throw"); |
| 15 | + var_dump($result); |
| 16 | +} catch (Exception $e) { |
| 17 | + var_dump($e->getMessage()); |
| 18 | +} |
| 19 | + |
| 20 | +try { |
| 21 | + $result = false && throw new Exception("false && throw"); |
| 22 | + var_dump($result); |
| 23 | +} catch (Exception $e) { |
| 24 | + var_dump($e->getMessage()); |
| 25 | +} |
| 26 | + |
| 27 | +try { |
| 28 | + $result = false || throw new Exception("false || throw"); |
| 29 | + var_dump($result); |
| 30 | +} catch (Exception $e) { |
| 31 | + var_dump($e->getMessage()); |
| 32 | +} |
| 33 | + |
| 34 | +try { |
| 35 | + $result = null ?? throw new Exception("null ?? throw"); |
| 36 | + var_dump($result); |
| 37 | +} catch (Exception $e) { |
| 38 | + var_dump($e->getMessage()); |
| 39 | +} |
| 40 | + |
| 41 | +try { |
| 42 | + $result = "foo" ?? throw new Exception('"foo" ?? throw'); |
| 43 | + var_dump($result); |
| 44 | +} catch (Exception $e) { |
| 45 | + var_dump($e->getMessage()); |
| 46 | +} |
| 47 | + |
| 48 | +try { |
| 49 | + $result = null ?: throw new Exception("null ?: throw"); |
| 50 | + var_dump($result); |
| 51 | +} catch (Exception $e) { |
| 52 | + var_dump($e->getMessage()); |
| 53 | +} |
| 54 | + |
| 55 | +try { |
| 56 | + $result = "foo" ?: throw new Exception('"foo" ?: throw'); |
| 57 | + var_dump($result); |
| 58 | +} catch (Exception $e) { |
| 59 | + var_dump($e->getMessage()); |
| 60 | +} |
| 61 | + |
| 62 | +try { |
| 63 | + $callable = fn() => throw new Exception("fn() => throw"); |
| 64 | + var_dump("not yet"); |
| 65 | + $callable(); |
| 66 | +} catch (Exception $e) { |
| 67 | + var_dump($e->getMessage()); |
| 68 | +} |
| 69 | + |
| 70 | +$result = "bar"; |
| 71 | +try { |
| 72 | + $result = throw new Exception(); |
| 73 | +} catch (Exception $e) {} |
| 74 | +var_dump($result); |
| 75 | + |
| 76 | +try { |
| 77 | + var_dump( |
| 78 | + throw new Exception("exception 1"), |
| 79 | + throw new Exception("exception 2") |
| 80 | + ); |
| 81 | +} catch (Exception $e) { |
| 82 | + var_dump($e->getMessage()); |
| 83 | +} |
| 84 | + |
| 85 | +try { |
| 86 | + $result = true ? true : throw new Exception("true ? true : throw"); |
| 87 | + var_dump($result); |
| 88 | +} catch (Exception $e) { |
| 89 | + var_dump($e->getMessage()); |
| 90 | +} |
| 91 | + |
| 92 | +try { |
| 93 | + $result = false ? true : throw new Exception("false ? true : throw"); |
| 94 | + var_dump($result); |
| 95 | +} catch (Exception $e) { |
| 96 | + var_dump($e->getMessage()); |
| 97 | +} |
| 98 | + |
| 99 | +try { |
| 100 | + throw new Exception() + 1; |
| 101 | +} catch (Throwable $e) { |
| 102 | + var_dump($e->getMessage()); |
| 103 | +} |
| 104 | + |
| 105 | +try { |
| 106 | + throw $exception = new Exception('throw $exception = new Exception();'); |
| 107 | +} catch (Exception $e) {} |
| 108 | +var_dump($exception->getMessage()); |
| 109 | + |
| 110 | +try { |
| 111 | + $exception = null; |
| 112 | + throw $exception ??= new Exception('throw $exception ??= new Exception();'); |
| 113 | +} catch (Exception $e) {} |
| 114 | +var_dump($exception->getMessage()); |
| 115 | + |
| 116 | +try { |
| 117 | + throw null ?? new Exception('throw null ?? new Exception();'); |
| 118 | +} catch (Exception $e) { |
| 119 | + var_dump($e->getMessage()); |
| 120 | +} |
| 121 | + |
| 122 | +?> |
| 123 | +--EXPECTF-- |
| 124 | +string(13) "true && throw" |
| 125 | +bool(true) |
| 126 | +bool(false) |
| 127 | +string(14) "false || throw" |
| 128 | +string(13) "null ?? throw" |
| 129 | +string(3) "foo" |
| 130 | +string(13) "null ?: throw" |
| 131 | +string(3) "foo" |
| 132 | +string(7) "not yet" |
| 133 | +string(13) "fn() => throw" |
| 134 | +string(3) "bar" |
| 135 | +string(11) "exception 1" |
| 136 | +bool(true) |
| 137 | +string(20) "false ? true : throw" |
| 138 | + |
| 139 | +Notice: Object of class Exception could not be converted to number in %s on line %d |
| 140 | +string(22) "Can only throw objects" |
| 141 | +string(35) "throw $exception = new Exception();" |
| 142 | +string(37) "throw $exception ??= new Exception();" |
| 143 | +string(30) "throw null ?? new Exception();" |
0 commit comments