Exception
extends Exception
in package
A class that serves as a base exception class with helpers to assist with errors/exceptions handling.
Example:
// throw an exception
$signature = 'YException:XException'; // YException extends YException and will get created if it does not exist.
Exception::throw($signature, $message, $code, $previous);
// handle the passed callback in a safe context where errors get converted to exceptions
Exception::handle($callback, $signature, $message);
// trigger an E_USER_* error, warning, notice, or deprecated with backtrace info
Exception::trigger($message, $severity);
Tags
Table of Contents
- __construct() : mixed
- Class constructor.
- __toString() : string
- Returns a string representation of the exception object.
- handle() : void
- Handles the passed callback in a safe context where PHP errors (and exceptions) result in exceptions that can be caught.
- throw() : void
- Throws an exception using the given signature.
- trigger() : void
- Triggers a user-level error, warning, notice, or deprecation with backtrace info.
- create() : string
- Creates an exception class dynamically and returns its class FQN.
Methods
__construct()
Class constructor.
public
__construct(string $message, int $code[, Throwable $previous = null ]) : mixed
Parameters
- $message : string
-
The Exception message.
- $code : int
- $previous : Throwable = null
Return values
mixed —__toString()
Returns a string representation of the exception object.
public
__toString() : string
Return values
string —handle()
Handles the passed callback in a safe context where PHP errors (and exceptions) result in exceptions that can be caught.
public
static handle(callable $callback[, string $signature = null ][, string $message = null ]) : void
Parameters
- $callback : callable
-
The callback to be executed.
- $signature : string = null
-
[optional] Exception signature. This can be a class FQN like
SomeException
orNamespace\SomeException
or a class FQN with a parent FQN likeNamespace\SomeException:RuntimeException
. Note that exception class will be created at runtime if it does not exist. - $message : string = null
-
[optional] The exception message if the callback raised an error or throw an exception.
Tags
Return values
void —throw()
Throws an exception using the given signature.
public
static throw(string $signature[, string $message = null ], int|string $code[, Throwable|null $previous = null ]) : void
NOTE:
Exceptions thrown via this method will be created at runtime if they are not build-in or defined explicitly (actual classes).
This means that the catch block that catches them must type-hint a fully qualified class name, because the use
statement
will trigger a call to the autoloader, and the autoloader may not know about the magic exception class at that point of time.
Parameters
- $signature : string
-
Exception signature. This can be a class FQN like
SomeException
orNamespace\SomeException
or a class FQN with a parent FQN likeNamespace\SomeException:RuntimeException
. Note that exception class will be created at runtime if it does not exist. - $message : string = null
-
[optional] Exception message. An auto-generated exception message will be created using backtrace if this is left empty.
- $code : int|string
-
[optional] Exception code. The code will be casted into an integer.
- $previous : Throwable|null = null
-
[optional] Previous exception.
Tags
Return values
void —trigger()
Triggers a user-level error, warning, notice, or deprecation with backtrace info.
public
static trigger(string $message[, int $severity = E_USER_ERROR ]) : void
Parameters
- $message : string
-
Error message.
- $severity : int = E_USER_ERROR
-
Error severity (
E_USER_*
family error). Default and fallback isE_USER_ERROR
.-
E_USER_ERROR => 256
, -
E_USER_WARNING => 512
, -
E_USER_NOTICE => 1024
, -
E_USER_DEPRECATED => 16384
.
-
Return values
void —create()
Creates an exception class dynamically and returns its class FQN.
protected
final static create(string $signature) : string
Parameters
- $signature : string