Exception handling
The framework features the
exceptionHandler function, that is intended to do global
exception handling. This function is used to catch and handle uncaught exceptions at a central
place. For this reason, it uses an
ExceptionHandler component, that can be configured
within the registry. The exception handler itself is then responsible for the handling of the current
exception. This mechanism is similar to the integrated
error handling.
The preconfigured APF exception handler saves the exception message into a log file and displays the
nice exception screen including the stacktrace. Details can be taken from the
API documentation of the
DefaultExceptionHandler class.
To replace the preconfigured
DefaultExceptionHandler with an own class, the registry
offset
ExceptionHandler within the
apf::core namespace must be filled
with the desired
ExceptionHandlerDefinition instance. The configuration of the exception
handling component must be placed in the bootstrap file directly after the
pagecontroller.php
is included.
The definition of a custom exception handler looks as follows:
PHP-Code
$reg = Singleton::getInstance('Registry');
$reg->register(
'apf::core',
'ExceptionHandler',
new ExceptionHandlerDefinition(
'my::exceptionhandler::namespace',
'MyExceptionHandler'
)
);
As you can take from the example, the desired
ExceptionHandler is referenced by it's
namespace and it's class name.
Note: if the registry offset does not contain a valid definition, the exceptions are
displayed in text mode. You can identify the fallback mode by the prefix
APF-Template
APF catchable exception:
In this cases, the definition or the implementation of the exception handler class must be checked.
In order to deactivate the ExceptionHandler, initilize the registry offset with
null.
To introduce a custom exception handling, you must implement the
AbstractExceptionHandler.
This class defines the following interface:
PHP-Code
abstract class AbstractExceptionHandler extends coreObject
{
function handleException($exception){
}
}
If an error occures, the
handleException is applied the current exception object
as an argument. Due to the fact, that the exception handling is done in an page controller independent
class, that is not created by the service manager, the current
Context and the current
Language are
not available!
If these information are important for the exception handling, the must be obtained from information
containers available within an application or module (e.g. application model class). The environment
param is available via the registry as mentioned in the corresponding documentation chapter.
The global exception handling mechanism can be used by placing the following PHP code within your
application:
PHP-Code
throw new Exception('My application exception');
Please note, that the global exception handler only is invoked, when the current exception is not
caught by a
try-catch block!
Details on the usage of exceptions can be taken from the online documentation under
http://php.net/exception and
http://php.net/exceptions.
Comments
Do you want to add a comment to the article above, or do you want to post additional hints? So please click
here. Comments already posted can be found below.
There are no comments belonging to this article.