PHP 8.5.10 Released!

Yaf_Dispatcher::catchException

(Yaf >=1.0.0)

Yaf_Dispatcher::catchExceptionSwitch on/off exception catching

Description

public function Yaf_Dispatcher::catchException(?bool $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 bool indicating whether exception catching is enabled when it is not.

Examples

Example #1 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.php

See Also

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top