A
A
artem782016-12-08 01:58:58
PHP
artem78, 2016-12-08 01:58:58

Why doesn't error trapping work?

Here is an example that causes the script to crash instead of handling the error.

<?php
error_reporting(E_ERROR);

echo "Начало\n";

try {
        $a->foo(); # Переменной $a не существует!
} catch (Exception $e) {
        echo "Произошла ошибка: " . $e->getMessage() . "\n";
}

echo "Конец\n";

Conclusion:
Start
PHP Fatal error: Call to a member function foo() on a non-object in /home/user/www/test/try.php on line 8

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Philipp, 2016-12-08
@zoonman

This works in PHP 7.
For lower versions, you need to use error_handler(), but for Fatal, it may not work, so solutions like register_shutdown_function() are sometimes used.
Read https://habrahabr.ru/post/261451/

A
Alexander Masterov, 2016-12-08
@AlexMasterov

An exception is not an error. But catching and handling (without crashing) a level error E_ERRORas an exception is possible only from PHP 7 version.
Error
Throwable
Throwable exception and errors in php7

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question