php8.5
Home/ Manual/ yaf / yaf_dispatcher/ Yaf_Dispatcher::catchException

Yaf_Dispatcher::catchException

PHP function Edit on GitHub ✎

(Yaf >=1.0.0)

Switch on/off exception catching

Description

Yaf_Dispatcher::catchException(bool|null $flag = null): Yaf_Dispatcher|bool

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

flag

Whether Yaf should catch uncaught exceptions and dispatch them to ErrorController::error.

Note

Since 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
<?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:

output
/* 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.php

See Also

  • Yaf_Dispatcher::throwException
  • Yaf_Dispatcher::setErrorHandler

Source: reference/yaf/yaf_dispatcher/catchexception.xml · from the official PHP manual (php/doc-en)