Skip to content
PHP Ecosystem
Explore

ErrorHandler

API reference › yii › base › ErrorHandler

abstract classyii\base\ErrorHandler
ExtendsComponent » BaseObject
ImplementsConfigurable
Since2.0
Sourceframework/base/ErrorHandler.php
2 subclasses

ErrorHandler handles uncaught PHP errors and exceptions.

ErrorHandler is configured as an application component in Application by default. You can access that instance via Yii::$app->errorHandler.

For more details and usage information on ErrorHandler, see the guide article on handling errors.

PropertyTypeDescriptionDefined by
$behaviors read-onlyarray<Behavior<static>>List of behaviors attached to this component.Component
$discardExistingOutputboolWhether to discard any existing page output before error display. Default: true.ErrorHandler
$exceptionThrowable|nullThe exception that is being handled currently. Default: null.ErrorHandler
$memoryReserveSizeintThe size of the reserved memory. Default: 262144.ErrorHandler
$silentExitOnExceptionboolIf true - handleException() will finish script with ExitCode::OK. false - ExitCode::UNSPECIFIED_ERROR. Default: null. Available since 2.0.36.ErrorHandler
MethodDescriptionDefined by
__construct()Constructor.BaseObject
__call()Calls the named method which is not a class method.Component
__clone()This method is called after the object is created by cloning an existing one.Component
__get()Returns the value of a component property.Component
__isset()Checks if a property is set, i.e. defined and not null.Component
__set()Sets the value of a component property.Component
__unset()Sets a component property to be null.Component
attachBehavior()Attaches a behavior to this component.Component
attachBehaviors()Attaches a list of behaviors to the component.Component
behaviors()Returns a list of behaviors that this component should behave as.Component
canGetProperty()Returns a value indicating whether a property can be read.Component
canSetProperty()Returns a value indicating whether a property can be set.Component
className() staticReturns the fully qualified name of this class.BaseObject
clearOutput()Removes all output echoed before calling this method.ErrorHandler
convertExceptionToString() staticConverts an exception into a simple string.ErrorHandler
convertExceptionToVerboseString() staticConverts an exception into a string that has verbose information about the exception and its trace.ErrorHandler
detachBehavior()Detaches a behavior from the component.Component
detachBehaviors()Detaches all behaviors from the component.Component
ensureBehaviors()Makes sure that the behaviors declared in behaviors() are attached to this component.Component
getBehavior()Returns the named behavior object.Component
getBehaviors()Returns all behaviors attached to this component.Component
handleError()Handles PHP execution errors such as warnings and notices.ErrorHandler
handleException()Handles uncaught PHP exceptions.ErrorHandler
handleFatalError()Handles fatal PHP errors.ErrorHandler
hasEventHandlers()Returns a value indicating whether there is any handler attached to the named event.Component
hasMethod()Returns a value indicating whether a method is defined.Component
hasProperty()Returns a value indicating whether a property is defined for this component.Component
init()ErrorHandler
logException()Logs the given exception.ErrorHandler
off()Detaches an existing event handler from this component.Component
on()Attaches an event handler to an event.Component
register()Register this error handler.ErrorHandler
trigger()Triggers an event.Component
unregister()Unregisters this error handler by restoring the PHP error and exception handlers.ErrorHandler
handleFallbackExceptionMessage() protectedHandles exception thrown during exception processing in handleException().ErrorHandler
renderException() protected abstractRenders the exception.ErrorHandler
EventTypeDescriptionDefined by
EVENT_SHUTDOWNEventAn event that is triggered when the handler is called by shutdown function via handleFatalError(). Event name: 'shutdown'. Available since 2.0.46.ErrorHandler
public bool $discardExistingOutput = true

Whether to discard any existing page output before error display.

Defaults to true.

public int $memoryReserveSize = 262144

The size of the reserved memory.

A portion of memory is pre-allocated so that when an out-of-memory issue occurs, the error handler is able to handle the error with the help of this reserved memory. If you set this value to be 0, no memory will be reserved. Defaults to 256KB.

public function clearOutput(): mixed

Removes all output echoed before calling this method.

Returns

mixed

public static function convertExceptionToString(Throwable $exception): string

Converts an exception into a simple string.

Parameters

ParameterTypeDescription
$exceptionThrowableThe exception being converted

Returns

string: The string representation of the exception.

public static function convertExceptionToVerboseString(Throwable $exception): string

Converts an exception into a string that has verbose information about the exception and its trace.

Parameters

ParameterTypeDescription
$exceptionThrowableThe exception being converted

Returns

string: The string representation of the exception.

Since

2.0.14

public function handleError(int $code, string $message, string $file, int $line): bool

Handles PHP execution errors such as warnings and notices.

This method is used as a PHP error handler. It will simply raise an ErrorException.

Parameters

ParameterTypeDescription
$codeintThe level of the error raised.
$messagestringThe error message.
$filestringThe filename that the error was raised in.
$lineintThe line number the error was raised at.

Returns

bool: Whether the normal error handler continues.

Throws

public function handleException(Throwable $exception): mixed

Handles uncaught PHP exceptions.

This method is implemented as a PHP exception handler.

Parameters

ParameterTypeDescription
$exceptionThrowableThe exception that is not caught

Returns

mixed

public function handleFatalError(): mixed

Handles fatal PHP errors.

Returns

mixed

public function init(): mixed

Returns

mixed

public function logException(Throwable $exception): mixed

Logs the given exception.

Parameters

ParameterTypeDescription
$exceptionThrowableThe exception to be logged

Returns

mixed

public function register(): mixed

Register this error handler.

Returns

mixed

public function unregister(): mixed

Unregisters this error handler by restoring the PHP error and exception handlers.

Returns

mixed

protected function handleFallbackExceptionMessage(Throwable $exception, Throwable $previousException): mixed

Handles exception thrown during exception processing in handleException().

Parameters

ParameterTypeDescription
$exceptionThrowableException that was thrown during main exception processing.
$previousExceptionThrowableMain exception processed in handleException().

Returns

mixed

Since

2.0.11

abstract protected function renderException(Throwable $exception): mixed

Renders the exception.

Parameters

ParameterTypeDescription
$exceptionThrowableThe exception to be rendered.

Returns

mixed