<!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>Switch on/off exception catching</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="yaf-dispatcher.autorender.html">Yaf_Dispatcher::autoRender</a></div> <div class="next" style="text-align: right; float: right;"><a href="yaf-dispatcher.clone.html">Yaf_Dispatcher::__clone</a></div> <div class="up"><a href="class.yaf-dispatcher.html">Yaf_Dispatcher</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="yaf-dispatcher.catchexception" class="refentry"> <div class="refnamediv"> <h1 class="refname">Yaf_Dispatcher::catchException</h1> <p class="verinfo">(Yaf >=1.0.0)</p><p class="refpurpose"><span class="refname">Yaf_Dispatcher::catchException</span> — <span class="dc-title">Switch on/off exception catching</span></p> </div> <div class="refsect1 description" id="refsect1-yaf-dispatcher.catchexception-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type"><a href="class.yaf-dispatcher.html" class="type Yaf_Dispatcher">Yaf_Dispatcher</a></span> <span class="methodname"><strong>Yaf_Dispatcher::catchException</strong></span> ([ <span class="methodparam"><span class="type">bool</span> <code class="parameter">$flag</code></span> ] )</div> <p class="para rdfs-comment"> While the application.dispatcher.throwException is On(you can also calling to <span class="methodname"><strong>Yaf_Dispatcher::throwException(TRUE)()</strong></span> to enable it), Yaf will throw Exception whe error occurrs instead of trigger error. </p> <p class="para"> then if you enable <span class="methodname"><strong>Yaf_Dispatcher::catchException()</strong></span>(also can enabled by set <a href="yaf.appconfig.html#configuration.yaf.dispatcher.catchexception" class="link">application.dispatcher.catchException</a>), all uncaught Exceptions will be caught by ErrorController::error if you have defined one. </p> </div> <div class="refsect1 parameters" id="refsect1-yaf-dispatcher.catchexception-parameters"> <h3 class="title">Parameters</h3> <dl> <dt> <span class="term"><em><code class="parameter">flag</code></em></span> <dd> <p class="para"> bool </p> </dd> </dt> </dl> </div> <div class="refsect1 returnvalues" id="refsect1-yaf-dispatcher.catchexception-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> </p> </div> <div class="refsect1 examples" id="refsect1-yaf-dispatcher.catchexception-examples"> <h3 class="title">Examples</h3> <div class="example" id="example-4127"> <p><strong>Example #1 <span class="function"><strong>Yaf_Dispatcher::catchException()</strong></span>example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> /* if you defined a ErrorController like following */<br /><span style="color: #0000BB"><?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">ErrorController </span><span style="color: #007700">extends </span><span style="color: #0000BB">Yaf_Controller_Abstract </span><span style="color: #007700">{<br /> </span><span style="color: #FF8000">/** <br /> * you can also call to Yaf_Request_Abstract::getException to get the <br /> * un-caught exception.<br /> */<br /> </span><span style="color: #007700">public function </span><span style="color: #0000BB">errorAction</span><span style="color: #007700">(</span><span style="color: #0000BB">$exception</span><span style="color: #007700">) {<br /> </span><span style="color: #FF8000">/* error occurs */<br /> </span><span style="color: #007700">switch (</span><span style="color: #0000BB">$exception</span><span style="color: #007700">-></span><span style="color: #0000BB">getCode</span><span style="color: #007700">()) {<br /> case </span><span style="color: #0000BB">YAF_ERR_NOTFOUND_MODULE</span><span style="color: #007700">:<br /> case </span><span style="color: #0000BB">YAF_ERR_NOTFOUND_CONTROLLER</span><span style="color: #007700">:<br /> case </span><span style="color: #0000BB">YAF_ERR_NOTFOUND_ACTION</span><span style="color: #007700">:<br /> case </span><span style="color: #0000BB">YAF_ERR_NOTFOUND_VIEW</span><span style="color: #007700">:<br /> echo </span><span style="color: #0000BB">404</span><span style="color: #007700">, </span><span style="color: #DD0000">":"</span><span style="color: #007700">, </span><span style="color: #0000BB">$exception</span><span style="color: #007700">-></span><span style="color: #0000BB">getMessage</span><span style="color: #007700">();<br /> break;<br /> default :<br /> </span><span style="color: #0000BB">$message </span><span style="color: #007700">= </span><span style="color: #0000BB">$exception</span><span style="color: #007700">-></span><span style="color: #0000BB">getMessage</span><span style="color: #007700">();<br /> echo </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #DD0000">":"</span><span style="color: #007700">, </span><span style="color: #0000BB">$exception</span><span style="color: #007700">-></span><span style="color: #0000BB">getMessage</span><span style="color: #007700">();<br /> break;<br /> }<br /> } <br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> /* now if some error occur, assuming access a non-exists controller(or you can throw a exception yourself): */ 404:Could not find controller script **/application/controllers/No-exists-controller.php </pre></div> </div> </div> </div> <div class="refsect1 seealso" id="refsect1-yaf-dispatcher.catchexception-seealso"> <h3 class="title">See Also</h3> <ul class="simplelist"> <li class="member"> <span class="methodname"><a href="yaf-dispatcher.throwexception.html" class="methodname" rel="rdfs-seeAlso">Yaf_Dispatcher::throwException()</a> - Switch on/off exception throwing</span></li> <li class="member"> <span class="methodname"><a href="yaf-dispatcher.seterrorhandler.html" class="methodname" rel="rdfs-seeAlso">Yaf_Dispatcher::setErrorHandler()</a> - Set error handler</span></li> </ul> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="yaf-dispatcher.autorender.html">Yaf_Dispatcher::autoRender</a></div> <div class="next" style="text-align: right; float: right;"><a href="yaf-dispatcher.clone.html">Yaf_Dispatcher::__clone</a></div> <div class="up"><a href="class.yaf-dispatcher.html">Yaf_Dispatcher</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>