Yaf_Dispatcher::catchException
(Yaf >=1.0.0)
Switch on/off exception catching
Description
While application.dispatcher.throwException is on (which can also be enabled by calling Yaf_Dispatcher::throwException), Yaf throws exceptions when errors occur instead of triggering errors.
If exception catching is additionally enabled via this method (or via application.dispatcher.catchException), all uncaught exceptions are caught and dispatched to ErrorController::error, if one is defined.
Parameters
flagWhether Yaf should catch uncaught exceptions and dispatch them to
ErrorController::error.NoteSince Yaf 2.2.0, if this parameter is not given, the current state is returned instead.
Return Values
Returns the Yaf_Dispatcher object itself when flag is given, or a Boolean indicating whether exception catching is enabled when it is not.
Examples
Yaf_Dispatcher::catchException() example
<?php
/* if you defined an ErrorController like following */
class ErrorController extends Yaf_Controller_Abstract {
/**
* you can also call Yaf_Request_Abstract::getException to get the
* uncaught exception.
*/
public function errorAction($exception) {
/* an error occurred */
switch ($exception->getCode()) {
case YAF_ERR_NOTFOUND_MODULE:
case YAF_ERR_NOTFOUND_CONTROLLER:
case YAF_ERR_NOTFOUND_ACTION:
case YAF_ERR_NOTFOUND_VIEW:
echo 404, ":", $exception->getMessage();
break;
default :
$message = $exception->getMessage();
echo 0, ":", $exception->getMessage();
break;
}
}
}
?>The above example will output something similar to:
/* now if some error occurs, assuming accessing a non-existent controller
(or you can throw an exception yourself): */
404:Could not find controller script **/application/controllers/No-exists-controller.phpSee Also
Yaf_Dispatcher::throwExceptionYaf_Dispatcher::setErrorHandler