In PHP 7 the base interface for any object that can be thrown is Throwable. We also have an Error base class for all internal PHP errors. But why then I can't catch errors like:
a)
try {
$file = fopen('not_exist_file', 'r');
} catch (\Error $e) {
echo 'Cannot open a file';
}
Expected result: 'Cannot open a file'
Actual result: PHP Warning: fopen(not_exist_file): failed to open stream: No such file or directory
b)
try {
$result = 10 / 0;
} catch(\DivisionByZeroError $e){
echo 'Catch DivisionByZeroError';
} catch (\Throwable $e) {
echo 'Catch Throwable';
}
Expected result: 'Catch DivisionByZeroError'
Actual result: PHP Warning: Division by zero in ..
c)
try {
trigger_error('User error');
} catch(\Error $e) {
echo 'Catch error';
} catch (\Throwable $e) {
echo 'Catch throwable';
}
Expected result: 'Catch error'
Actual result: PHP Notice: User error in ..
My PHP version 7.1.1 (cli)