"Fossies" - the Fresh Open Source Software Archive

Member "php-8.0.28-src/Zend/tests/throw/001.phpt" (14 Feb 2023, 3759 Bytes) of package /windows/www/php-8.0.28-src.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    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 = false && throw new Exception("false && throw");
   15     var_dump($result);
   16 } catch (Exception $e) {
   17     var_dump($e->getMessage());
   18 }
   19 
   20 try {
   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 
   27 try {
   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 
   34 try {
   35     $result = true || throw new Exception("true || throw");
   36     var_dump($result);
   37 } catch (Exception $e) {
   38     var_dump($e->getMessage());
   39 }
   40 
   41 try {
   42     $result = false || throw new Exception("false || throw");
   43     var_dump($result);
   44 } catch (Exception $e) {
   45     var_dump($e->getMessage());
   46 }
   47 
   48 try {
   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 
   55 try {
   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 
   62 try {
   63     $result = null ?? throw new Exception("null ?? throw");
   64     var_dump($result);
   65 } catch (Exception $e) {
   66     var_dump($e->getMessage());
   67 }
   68 
   69 try {
   70     $result = "foo" ?? throw new Exception('"foo" ?? throw');
   71     var_dump($result);
   72 } catch (Exception $e) {
   73     var_dump($e->getMessage());
   74 }
   75 
   76 try {
   77     $result = null ?: throw new Exception("null ?: throw");
   78     var_dump($result);
   79 } catch (Exception $e) {
   80     var_dump($e->getMessage());
   81 }
   82 
   83 try {
   84     $result = "foo" ?: throw new Exception('"foo" ?: throw');
   85     var_dump($result);
   86 } catch (Exception $e) {
   87     var_dump($e->getMessage());
   88 }
   89 
   90 try {
   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";
   99 try {
  100     $result = throw new Exception();
  101 } catch (Exception $e) {}
  102 var_dump($result);
  103 
  104 try {
  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 
  113 try {
  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 
  120 try {
  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 
  127 try {
  128     throw new Exception() + 1;
  129 } catch (Throwable $e) {
  130     var_dump($e->getMessage());
  131 }
  132 
  133 try {
  134     throw $exception = new Exception('throw $exception = new Exception();');
  135 } catch (Exception $e) {}
  136 var_dump($exception->getMessage());
  137 
  138 try {
  139     $exception = null;
  140     throw $exception ??= new Exception('throw $exception ??= new Exception();');
  141 } catch (Exception $e) {}
  142 var_dump($exception->getMessage());
  143 
  144 try {
  145     throw null ?? new Exception('throw null ?? new Exception();');
  146 } catch (Exception $e) {
  147     var_dump($e->getMessage());
  148 }
  149 
  150 ?>
  151 --EXPECT--
  152 string(13) "true && throw"
  153 bool(false)
  154 string(14) "true and throw"
  155 bool(false)
  156 bool(true)
  157 string(14) "false || throw"
  158 bool(true)
  159 string(14) "false or throw"
  160 string(13) "null ?? throw"
  161 string(3) "foo"
  162 string(13) "null ?: throw"
  163 string(3) "foo"
  164 string(7) "not yet"
  165 string(13) "fn() => throw"
  166 string(3) "bar"
  167 string(11) "exception 1"
  168 bool(true)
  169 string(20) "false ? true : throw"
  170 string(42) "Unsupported operand types: Exception + int"
  171 string(35) "throw $exception = new Exception();"
  172 string(37) "throw $exception ??= new Exception();"
  173 string(30) "throw null ?? new Exception();"