<!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 >= 5.5.0)</p><p class="refpurpose"><span class="refname">Generator::throw</span> — <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.exception.html" class="type Exception">Exception</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's context instead. </p> </div> <div class="refsect1 parameters" id="refsect1-generator.throw-parameters"> <h3 class="title">Parameters</h3> <dl> <dt> <span class="term"><em><code class="parameter">exception</code></em></span> <dd> <p class="para"> Exception to throw into the generator. </p> </dd> </dt> </dl> </div> <div class="refsect1 examples" id="refsect1-generator.throw-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-310"> <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"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">gen</span><span style="color: #007700">() {<br /> echo </span><span style="color: #DD0000">"Foo\n"</span><span style="color: #007700">;<br /> try {<br /> </span><span style="color: #0000BB">yield</span><span style="color: #007700">;<br /> } catch (</span><span style="color: #0000BB">Exception $e</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Exception: </span><span style="color: #007700">{</span><span style="color: #0000BB">$e</span><span style="color: #007700">-></span><span style="color: #0000BB">getMessage</span><span style="color: #007700">()}</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br /> }<br /> echo </span><span style="color: #DD0000">"Bar\n"</span><span style="color: #007700">;<br />}<br /> <br /></span><span style="color: #0000BB">$gen </span><span style="color: #007700">= </span><span style="color: #0000BB">gen</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$gen</span><span style="color: #007700">-></span><span style="color: #0000BB">rewind</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$gen</span><span style="color: #007700">-></span><span style="color: #0000BB">throw</span><span style="color: #007700">(new </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">?></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 class="refsect1 returnvalues" id="refsect1-generator.throw-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the yielded value. </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>