try{ 1/0; }catch (\DivisionByZeroError $error){ echo $error->getMessage(); } 

In theory, it should work catch, but for some reason displays

Warning: Division by zero in C: \ path \ index.php on line 4

    1 answer 1

    Dividing by zero is a syntax error. Therefore, it is properly handled as in the example , using throw :

     function inverse($x) { if (!$x) { throw new Exception('Деление на ноль.'); } return 1/$x; } try { echo inverse(5) . "\n"; echo inverse(0) . "\n"; } catch (Exception $e) { echo 'Выброшено исключение: ', $e->getMessage(), "\n"; }