Sophie

Sophie

distrib > Mageia > 7 > armv7hl > by-pkgid > 2b917e0437961edec048f1d15e2d7449 > files > 7424

php-manual-en-7.2.11-1.mga7.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Throw an exception into the generator</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="generator.send.html">Generator::send</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="generator.valid.html">Generator::valid</a></div>
 <div class="up"><a href="class.generator.html">Generator</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="generator.throw" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">Generator::throw</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.5.0, PHP 7)</p><p class="refpurpose"><span class="refname">Generator::throw</span> &mdash; <span class="dc-title">Throw an exception into the generator</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-generator.throw-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>Generator::throw</strong></span>
    ( <span class="methodparam"><span class="type"><a href="class.throwable.html" class="type Throwable">Throwable</a></span> <code class="parameter">$exception</code></span>
   )</div>

  <p class="para rdfs-comment">
   Throws an exception into the generator and resumes execution of the generator.
   The behavior will be the same as if the current <a href="language.generators.syntax.html#control-structures.yield" class="link">yield</a> expression was replaced with
   a <em>throw $exception</em> statement.
  </p>
  <p class="para">
   If the generator is already closed when this method is invoked, the exception will
   be thrown in the caller&#039;s context instead.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-generator.throw-parameters">
  <h3 class="title">Parameters</h3>
  <dl>

   
    <dt>
<code class="parameter">exception</code></dt>

    <dd>

     <p class="para">
      Exception to throw into the generator.
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-generator.throw-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns the yielded value.
  </p>
 </div>

 
 <div class="refsect1 changelog" id="refsect1-generator.throw-changelog">
  <h3 class="title">Changelog</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Version</th>
       <th>Description</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>7.0.0</td>
       <td>
        The <code class="parameter">exception</code> parameter also accepts <a href="class.throwable.html" class="classname">Throwable</a> now.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 
 <div class="refsect1 examples" id="refsect1-generator.throw-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-334">
    <p><strong>Example #1 Throwing an exception into a generator</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">gen</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Foo\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;try&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">yield</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;catch&nbsp;(</span><span style="color: #0000BB">Exception&nbsp;$e</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Exception:&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">()}</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Bar\n"</span><span style="color: #007700">;<br />}<br />&nbsp;<br /></span><span style="color: #0000BB">$gen&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">gen</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$gen</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">rewind</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$gen</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">throw</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">Exception</span><span style="color: #007700">(</span><span style="color: #DD0000">'Test'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Foo
Exception: Test
Bar
</pre></div>
    </div>
   </div>
  </p>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="generator.send.html">Generator::send</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="generator.valid.html">Generator::valid</a></div>
 <div class="up"><a href="class.generator.html">Generator</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>