Skip to content

Instantly share code, notes, and snippets.

@missoxd
Last active April 12, 2024 14:28
Show Gist options
  • Save missoxd/bfc79e7a6e2a5e69f47a5e7da61a9806 to your computer and use it in GitHub Desktop.
Save missoxd/bfc79e7a6e2a5e69f47a5e7da61a9806 to your computer and use it in GitHub Desktop.
<?php
/*
\Error implements \Throwable
\Exception implements \Throwable
catch (\Error $e) - Will only catch \Error exceptions and children
catch (\Exception $e) - Will only catch \Exception exceptions and children
catch (\Throwable $e) - Will catch all
- - - - - - - - - -
@see: https://www.php.net/manual/en/language.errors.php7.php
PHP7 vs PHP5
PHP 7 changes how most errors are reported by PHP. Instead of reporting errors through the traditional error reporting
mechanism used by PHP 5, most errors are now reported by throwing Error exceptions.
As with normal exceptions, these Error exceptions will bubble up until they reach the first matching catch block. If there
are no matching blocks, then any default exception handler installed with set_exception_handler() will be called, and if
there is no default exception handler, then the exception will be converted to a fatal error and will be handled like a
traditional error.
As the Error hierarchy does not inherit from Exception, code that uses catch (Exception $e) { ... } blocks to handle uncaught
exceptions in PHP 5 will find that these Errors are not caught by these blocks. Either a catch (Error $e) { ... } block or a
set_exception_handler() handler is required.
- - - - - - - - - -
@see: https://www.php.net/manual/en/errorfunc.constants.php
Constants
1 - E_ERROR (int) - Fatal run-time errors. These indicate errors that can not be recovered from, such as a memory allocation problem. Execution of the script is halted.
2 - E_WARNING (int) - Run-time warnings (non-fatal errors). Execution of the script is not halted.
4 - E_PARSE (int) - Compile-time parse errors. Parse errors should only be generated by the parser.
8 - E_NOTICE (int) - Run-time notices. Indicate that the script encountered something that could indicate an error, but could also happen in the normal course of running a script.
16 - E_CORE_ERROR (int) - Fatal errors that occur during PHP's initial startup. This is like an E_ERROR, except it is generated by the core of PHP.
32 - E_CORE_WARNING (int) - Warnings (non-fatal errors) that occur during PHP's initial startup. This is like an E_WARNING, except it is generated by the core of PHP.
64 - E_COMPILE_ERROR (int) - Fatal compile-time errors. This is like an E_ERROR, except it is generated by the Zend Scripting Engine.
128 - E_COMPILE_WARNING (int) - Compile-time warnings (non-fatal errors). This is like an E_WARNING, except it is generated by the Zend Scripting Engine.
256 - E_USER_ERROR (int) - User-generated error message. This is like an E_ERROR, except it is generated in PHP code by using the PHP function trigger_error().
512 - E_USER_WARNING (int) - User-generated warning message. This is like an E_WARNING, except it is generated in PHP code by using the PHP function trigger_error().
1024 - E_USER_NOTICE (int) - User-generated notice message. This is like an E_NOTICE, except it is generated in PHP code by using the PHP function trigger_error().
2048 - E_STRICT (int) - Enable to have PHP suggest changes to your code which will ensure the best interoperability and forward compatibility of your code.
4096 - E_RECOVERABLE_ERROR (int) - Catchable fatal error. It indicates that a probably dangerous error occurred, but did not leave the Engine in an unstable state. If the error is not caught by a user defined handle (see also set_error_handler()), the application aborts as it was an E_ERROR.
8192 - E_DEPRECATED (int) - Run-time notices. Enable this to receive warnings about code that will not work in future versions.
16384 - E_USER_DEPRECATED (int) - User-generated warning message. This is like an E_DEPRECATED, except it is generated in PHP code by using the PHP function trigger_error().
32767 - E_ALL (int) - All errors, warnings, and notices.
- - - - - - - - - -
@see: https://www.php.net/manual/en/function.set-error-handler
The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING,
E_COMPILE_ERROR, E_COMPILE_WARNING independent of where they were raised, and most of E_STRICT raised in the file where
set_error_handler() is called.
- - - - - - - - - -
If both set_error_handler and set_exception_handler are set and an error that both handlers can handle, the handle priority will be given for the first one that was registered.
*/
<?php
/*
- PHP 8.2
- Undefined function (E_ERROR)
- Without \Error catch
- Without \Exception catch
- Without register_shutdown_function
- Without set_error_handler
- Without set_exception_handler
*/
echo 'App - Init' . PHP_EOL;
what_is_this_function();
echo 'App - After error' . PHP_EOL;
echo 'App - final' . PHP_EOL;
/*
$ php -f test.php ; echo 'Bash exit code:' $?
App - Init
PHP Fatal error: Uncaught Error: Call to undefined function what_is_this_function() in /home/.../test.php:3
Stack trace:
#0 {main}
thrown in /home/.../test.php on line 3
Bash exit code: 255
*/
<?php
/*
- PHP 8.2
- Undefined function (E_ERROR)
- Without \Error catch
- Without \Exception catch
- With register_shutdown_function
- With set_error_handler
- Without set_exception_handler
*/
register_shutdown_function(function () {
echo '1 - Custom register_shutdown_function' . PHP_EOL;
var_dump(error_get_last());
});
// Not fired. Undefined function is an E_ERROR.
set_error_handler(function (int $errNo, string $errMsg, string $file, int $line) {
echo '1 - Custom set_error_handler' . PHP_EOL;
var_dump(compact('errNo', 'errMsg', 'file', 'line'));
});
echo 'App - Init' . PHP_EOL;
what_is_this_function();
echo 'App - After error' . PHP_EOL;
echo 'App - final' . PHP_EOL;
/*
$ php -f test.php ; echo 'Bash exit code:' $?
App - Init
PHP Fatal error: Uncaught Error: Call to undefined function what_is_this_function() in /home/.../test.php:13
Stack trace:
#0 {main}
thrown in /home/.../test.php on line 13
1 - Custom register_shutdown_function
array(4) {
["type"]=>
int(1)
["message"]=>
string(171) "Uncaught Error: Call to undefined function what_is_this_function() in /home/.../test.php:13
*/
<?php
/*
- PHP 8.2
- Undefined function (E_ERROR)
- Without \Error catch
- Without \Exception catch
- With register_shutdown_function
- With set_error_handler
- With set_exception_handler
*/
register_shutdown_function(function () {
echo '1 - Custom register_shutdown_function' . PHP_EOL;
var_dump(error_get_last());
});
// Not fired. Undefined function is an E_ERROR.
set_error_handler(function (int $errNo, string $errMsg, string $file, int $line) {
echo '1 - Custom set_error_handler' . PHP_EOL;
var_dump(compact('errNo', 'errMsg', 'file', 'line'));
});
set_exception_handler(function (\Throwable $e) {
echo '1 - Custom set_exception_handler' . PHP_EOL;
var_dump($e);
});
echo 'App - Init' . PHP_EOL;
what_is_this_function();
echo 'App - After error' . PHP_EOL;
echo 'App - final' . PHP_EOL;
/*
$ php -f test.php ; echo 'Bash exit code:' $?
App - Init
1 - Custom set_exception_handler
object(Error)#4 (7) {
["message":protected]=>
string(50) "Call to undefined function what_is_this_function()"
["string":"Error":private]=>
string(0) ""
["code":protected]=>
int(0)
["file":protected]=>
string(66) "/home/.../test.php"
["line":protected]=>
int(18)
["trace":"Error":private]=>
array(0) {
}
["previous":"Error":private]=>
NULL
}
1 - Custom register_shutdown_function
NULL
Bash exit code: 0
*/
<?php
/*
- PHP 8.2
- Undefined function (E_ERROR)
- Without \Error catch
- With \Exception catch
- Without register_shutdown_function
- Without set_error_handler
- Without set_exception_handler
*/
echo 'App - Init' . PHP_EOL;
try {
what_is_this_function();
echo 'App - On try block after error' . PHP_EOL;
} catch (\Exception $e) {
echo 'App - On catch \Exception block' . PHP_EOL;
}
echo 'App - After error' . PHP_EOL;
echo 'App - final' . PHP_EOL;
/*
$ php -f test.php ; echo 'Bash exit code:' $?
App - Init
PHP Fatal error: Uncaught Error: Call to undefined function what_is_this_function() in /home/.../test.php:3
Stack trace:
#0 {main}
thrown in /home/.../test.php on line 3
Bash exit code: 255
*/
<?php
/*
- PHP 8.2
- Undefined function (E_ERROR)
- With \Error catch
- Without \Exception catch
- Without register_shutdown_function
- Without set_error_handler
- Without set_exception_handler
*/
echo 'App - Init' . PHP_EOL;
try {
what_is_this_function();
echo 'App - On try block after error' . PHP_EOL;
} catch (\Error $e) {
echo 'App - On catch \Error block' . PHP_EOL;
}
echo 'App - After error' . PHP_EOL;
echo 'App - final' . PHP_EOL;
/*
$ php -f test.php ; echo 'Bash exit code:' $?
App - Init
App - On catch \Error block
App - After error
App - final
Bash exit code: 0
*/
<?php
/*
- PHP 8.2
- Undefined function (E_ERROR)
- With \Error catch
- Without \Exception catch
- With register_shutdown_function
- Without set_error_handler
- Without set_exception_handler
*/
register_shutdown_function(function () {
echo '1 - Custom register_shutdown_function' . PHP_EOL;
var_dump(error_get_last());
});
echo 'App - Init' . PHP_EOL;
try {
what_is_this_function();
echo 'App - On try block after error' . PHP_EOL;
} catch (\Error $e) {
echo 'App - On catch \Error block' . PHP_EOL;
}
echo 'App - After error' . PHP_EOL;
echo 'App - final' . PHP_EOL;
/*
$ php -f test.php ; echo 'Bash exit code:' $?
App - Init
App - On catch \Error block
App - After error
App - final
1 - Custom register_shutdown_function
NULL
Bash exit code: 0
*/
<?php
/*
- PHP 8.2
- Parse error (E_PARSE)
- With \Error catch
- Without \Exception catch
- Without register_shutdown_function
- Without set_error_handler
- Without set_exception_handler
*/
echo 'App - Init' . PHP_EOL;
try {
what is this
what_is_this_function();
echo 'App - On try block after error' . PHP_EOL;
} catch (\Error $e) {
echo 'App - On catch \Error block' . PHP_EOL;
}
echo 'App - After error' . PHP_EOL;
echo 'App - final' . PHP_EOL;
/*
$ php -f test.php ; echo 'Bash exit code:' $?
PHP Parse error: syntax error, unexpected identifier "is" in /home/.../test.php on line 4
Bash exit code: 255
*/
<?php
/*
- PHP 8.2
- Throw \Exception (E_ERROR)
- Without \Error catch
- Without \Exception catch
- Without register_shutdown_function
- Without set_error_handler
- Without set_exception_handler
*/
echo 'App - Init' . PHP_EOL;
throw new \Exception('what is this exception');
echo 'App - After exception' . PHP_EOL;
echo 'App - final' . PHP_EOL;
/*
$ php -f test.php ; echo 'Bash exit code:' $?
App - Init
PHP Fatal error: Uncaught Exception: what is this exception in /home/.../test.php:3
Stack trace:
#0 {main}
thrown in /home/.../test.php on line 3
Bash exit code: 255
*/
<?php
/*
- PHP 8.2
- Throw \Exception (E_ERROR)
- Without \Error catch
- Without \Exception catch
- With register_shutdown_function
- Without set_error_handler
- Without set_exception_handler
*/
register_shutdown_function(function () {
echo '1 - Custom register_shutdown_function' . PHP_EOL;
var_dump(error_get_last());
});
echo 'App - Init' . PHP_EOL;
throw new \Exception('what is this exception');
echo 'App - After exception' . PHP_EOL;
echo 'App - final' . PHP_EOL;
/*
$ php -f test.php ; echo 'Bash exit code:' $?
App - Init
PHP Fatal error: Uncaught Exception: what is this exception in /home/.../test.php:18
Stack trace:
#0 {main}
thrown in /home/.../test.php on line 18
1 - Custom register_shutdown_function
array(4) {
["type"]=>
int(1)
["message"]=>
string(147) "Uncaught Exception: what is this exception in /home/.../test.php:18
Stack trace:
#0 {main}
thrown"
["file"]=>
string(66) "/home/.../test.php"
["line"]=>
int(18)
}
Bash exit code: 255
*/
<?php
/*
- PHP 8.2
- Throw \Exception (E_ERROR)
- Without \Error catch
- Without \Exception catch
- With register_shutdown_function
- Without set_error_handler
- With set_exception_handler
*/
register_shutdown_function(function () {
echo '1 - Custom register_shutdown_function' . PHP_EOL;
var_dump(error_get_last());
});
set_exception_handler(function (\Throwable $e) {
echo '1 - Custom set_exception_handler' . PHP_EOL;
var_dump($e);
});
echo 'App - Init' . PHP_EOL;
throw new \Exception('what is this exception');
echo 'App - After exception' . PHP_EOL;
echo 'App - final' . PHP_EOL;
/*
$ php -f test.php ; echo 'Bash exit code:' $?
App - Init
1 - Custom set_exception_handler
object(Exception)#3 (7) {
["message":protected]=>
string(22) "what is this exception"
["string":"Exception":private]=>
string(0) ""
["code":protected]=>
int(0)
["file":protected]=>
string(66) "/home/.../test.php"
["line":protected]=>
int(13)
["trace":"Exception":private]=>
array(0) {
}
["previous":"Exception":private]=>
NULL
}
1 - Custom register_shutdown_function
NULL
Bash exit code: 0
*/
<?php
/*
- PHP 8.2
- Throw \Exception (E_ERROR)
- Without \Error catch
- With \Exception catch
- Without register_shutdown_function
- Without set_error_handler
- Without set_exception_handler
*/
echo 'App - Init' . PHP_EOL;
try {
throw new \Exception('what is this exception');
echo 'App - On try block after error' . PHP_EOL;
} catch (\Exception $e) {
echo 'App - On catch \Exception block' . PHP_EOL;
}
echo 'App - After exception' . PHP_EOL;
echo 'App - final' . PHP_EOL;
/*
$ php -f test.php ; echo 'Bash exit code:' $?
App - Init
App - On catch \Exception block
App - After exception
App - final
Bash exit code: 0
*/
<?php
/*
- PHP 8.2
- Throw \Exception (E_ERROR)
- Without \Error catch
- With \Exception catch
- With register_shutdown_function
- Without set_error_handler
- With set_exception_handler
*/
register_shutdown_function(function () {
echo '1 - Custom register_shutdown_function' . PHP_EOL;
var_dump(error_get_last());
});
// Not fired. \Exception was catched on code.
set_exception_handler(function (\Throwable $e) {
echo '1 - Custom set_exception_handler' . PHP_EOL;
var_dump($e);
});
echo 'App - Init' . PHP_EOL;
try {
throw new \Exception('what is this exception');
echo 'App - On try block after error' . PHP_EOL;
} catch (\Exception $e) {
echo 'App - On catch \Exception block' . PHP_EOL;
}
echo 'App - After exception' . PHP_EOL;
echo 'App - final' . PHP_EOL;
/*
$ php -f test.php ; echo 'Bash exit code:' $?
App - Init
App - On catch \Exception block
App - After exception
App - final
1 - Custom register_shutdown_function
NULL
Bash exit code: 0
*/
<?php
/*
- PHP 8.2
- Undefined variable (E_WARNING)
- Without \Error catch
- Without \Exception catch
- Without register_shutdown_function
- Without set_error_handler
- Without set_exception_handler
*/
echo 'App - Init' . PHP_EOL;
echo $what_is_this_variable;
echo 'App - After exception' . PHP_EOL;
echo 'App - final' . PHP_EOL;
/*
$ php -f test.php ; echo 'Bash exit code:' $?
App - Init
PHP Warning: Undefined variable $what_is_this_variable in /home/.../test.php on line 13
App - After exception
App - final
Bash exit code: 0
*/
<?php
/*
- PHP 8.2
- Undefined variable (E_WARNING)
- Without \Error catch
- Without \Exception catch
- With register_shutdown_function
- Without set_error_handler
- Without set_exception_handler
*/
register_shutdown_function(function () {
echo '1 - Custom register_shutdown_function' . PHP_EOL;
var_dump(error_get_last());
});
echo 'App - Init' . PHP_EOL;
echo $what_is_this_variable;
echo 'App - After exception' . PHP_EOL;
echo 'App - final' . PHP_EOL;
/*
$ php -f test.php ; echo 'Bash exit code:' $?
App - Init
PHP Warning: Undefined variable $what_is_this_variable in /home/.../test.php on line 18
App - After exception
App - final
1 - Custom register_shutdown_function
array(4) {
["type"]=>
int(2)
["message"]=>
string(41) "Undefined variable $what_is_this_variable"
["file"]=>
string(66) "/home/.../test.php"
["line"]=>
int(18)
}
Bash exit code: 0
*/
<?php
/*
- PHP 8.2
- Undefined variable (E_WARNING)
- Without \Error catch
- Without \Exception catch
- With register_shutdown_function
- With set_error_handler
- Without set_exception_handler
*/
register_shutdown_function(function () {
echo '1 - Custom register_shutdown_function' . PHP_EOL;
var_dump(error_get_last());
});
// Fired instead of the E_WARNING line.
set_error_handler(function (int $errNo, string $errMsg, string $file, int $line) {
echo '1 - Custom set_error_handler' . PHP_EOL;
var_dump(compact('errNo', 'errMsg', 'file', 'line'));
});
echo 'App - Init' . PHP_EOL;
echo $what_is_this_variable;
echo 'App - After exception' . PHP_EOL;
echo 'App - final' . PHP_EOL;
/*
$ php -f test.php ; echo 'Bash exit code:' $?
App - Init
1 - Custom set_error_handler
array(4) {
["errNo"]=>
int(2)
["errMsg"]=>
string(41) "Undefined variable $what_is_this_variable"
["file"]=>
string(66) "/home/.../test.php"
["line"]=>
int(23)
}
App - After exception
App - final
1 - Custom register_shutdown_function
NULL
Bash exit code: 0
*/
<?php
/*
- PHP 8.2
- Undefined variable (E_WARNING)
- Without \Error catch
- Without \Exception catch
- With register_shutdown_function
- Without set_error_handler
- With set_exception_handler
*/
register_shutdown_function(function () {
echo '1 - Custom register_shutdown_function' . PHP_EOL;
var_dump(error_get_last());
});
// Not fired on E_WARNING.
set_exception_handler(function (\Throwable $e) {
echo '1 - Custom set_exception_handler' . PHP_EOL;
var_dump($e);
});
echo 'App - Init' . PHP_EOL;
echo $what_is_this_variable;
echo 'App - After exception' . PHP_EOL;
echo 'App - final' . PHP_EOL;
/*
$ php -f test.php ; echo 'Bash exit code:' $?
App - Init
PHP Warning: Undefined variable $what_is_this_variable in /home/missoxd/Projects/testing-features-php-error-handler/test.php on line 23
App - After exception
App - final
1 - Custom register_shutdown_function
array(4) {
["type"]=>
int(2)
["message"]=>
string(41) "Undefined variable $what_is_this_variable"
["file"]=>
string(66) "/home/missoxd/Projects/testing-features-php-error-handler/test.php"
["line"]=>
int(23)
}
Bash exit code: 0
*/
<?php
/*
- PHP 8.2
- Undefined variable (E_WARNING)
- With \Error catch
- Without \Exception catch
- With register_shutdown_function
- Without set_error_handler
- Without set_exception_handler
*/
register_shutdown_function(function () {
echo '1 - Custom register_shutdown_function' . PHP_EOL;
var_dump(error_get_last());
});
echo 'App - Init' . PHP_EOL;
try {
echo $what_is_this_variable;
} catch (\Error $e) {
echo 'App - On catch \Error block' . PHP_EOL;
}
echo 'App - After exception' . PHP_EOL;
echo 'App - final' . PHP_EOL;
/*
$ php -f test.php ; echo 'Bash exit code:' $?
App - Init
PHP Warning: Undefined variable $what_is_this_variable in /home/.../test.php on line 19
App - After exception
App - final
1 - Custom register_shutdown_function
array(4) {
["type"]=>
int(2)
["message"]=>
string(41) "Undefined variable $what_is_this_variable"
["file"]=>
string(66) "/home/.../test.php"
["line"]=>
int(19)
}
Bash exit code: 0
*/
<?php
/*
- PHP 8.2
- Undefined variable (E_WARNING)
- Without \Error catch
- Without \Exception catch
- With register_shutdown_function
- With set_error_handler (throwing \Exception E_ERROR)
- Without set_exception_handler
*/
register_shutdown_function(function () {
echo '1 - Custom register_shutdown_function' . PHP_EOL;
var_dump(error_get_last());
});
// Fired instead of the E_WARNING line.
set_error_handler(function (int $errNo, string $errMsg, string $file, int $line) {
echo '1 - Custom set_error_handler' . PHP_EOL;
var_dump(compact('errNo', 'errMsg', 'file', 'line'));
throw new \Exception($errMsg, $errNo);
});
echo 'App - Init' . PHP_EOL;
echo $what_is_this_variable;
echo 'App - After exception' . PHP_EOL;
echo 'App - final' . PHP_EOL;
/*
$ php -f test.php ; echo 'Bash exit code:' $?
App - Init
1 - Custom set_error_handler
array(4) {
["errNo"]=>
int(2)
["errMsg"]=>
string(41) "Undefined variable $what_is_this_variable"
["file"]=>
string(66) "/home/.../test.php"
["line"]=>
int(25)
}
PHP Fatal error: Uncaught Exception: Undefined variable $what_is_this_variable in /home/.../test.php:21
Stack trace:
#0 /home/.../test.php(25): {closure}()
#1 {main}
thrown in /home/.../test.php on line 21
1 - Custom register_shutdown_function
array(4) {
["type"]=>
int(1)
["message"]=>
string(253) "Uncaught Exception: Undefined variable $what_is_this_variable in /home/.../test.php:21
Stack trace:
#0 /home/.../test.php(25): {closure}()
#1 {main}
thrown"
["file"]=>
string(66) "/home/.../test.php"
["line"]=>
int(21)
}
Bash exit code: 255
*/
<?php
/*
- PHP 8.2
- Undefined variable (E_WARNING)
- Without \Error catch
- With \Exception catch
- With register_shutdown_function
- With set_error_handler (throwing \Exception E_ERROR)
- Without set_exception_handler
*/
register_shutdown_function(function () {
echo '1 - Custom register_shutdown_function' . PHP_EOL;
var_dump(error_get_last());
});
// Fired instead of the E_WARNING line.
set_error_handler(function (int $errNo, string $errMsg, string $file, int $line) {
echo '1 - Custom set_error_handler' . PHP_EOL;
var_dump(compact('errNo', 'errMsg', 'file', 'line'));
throw new \Exception($errMsg, $errNo);
});
echo 'App - Init' . PHP_EOL;
try {
echo $what_is_this_variable;
} catch (\Exception $e) {
echo 'App - On catch \Exception block' . PHP_EOL;
}
echo 'App - After exception' . PHP_EOL;
echo 'App - final' . PHP_EOL;
/*
$ php -f test.php ; echo 'Bash exit code:' $?
App - Init
1 - Custom set_error_handler
array(4) {
["errNo"]=>
int(2)
["errMsg"]=>
string(41) "Undefined variable $what_is_this_variable"
["file"]=>
string(66) "/home/.../test.php"
["line"]=>
int(26)
}
App - On catch \Exception block
App - After exception
App - final
1 - Custom register_shutdown_function
NULL
Bash exit code: 0
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment