0
try{
//division by zero
$number = 5/0;
}
catch(Exception $ex){
echo 'Got it!';
}

Warning: Division by zero in D:\Sistemim\Server\WebSite\mckportal\test\hatakont.php on line 13

What could be the reason? How can I fix? He was working with PHP 5.3. The 7.0.4 version does not work.

cArf
  • 81
  • 1
  • 5
  • 3
    "He was working with PHP 5.3" - [really?](https://3v4l.org/N60N9) Maybe you had an [error handler](http://docs.php.net/set_error_handler) set up that "converted" the warning into an exception. – VolkerK Mar 10 '16 at 11:23
  • [Demo](https://3v4l.org/0OQLi) – Mark Baker Mar 10 '16 at 11:40
  • Have you read [this](http://php.net/manual/en/class.divisionbyzeroerror.php#118928)? – Egg Mar 10 '16 at 11:57
  • The comment on the PHP site linked by @Egg is misleading. And the sample code doesn't work. It defines an error handler but it doesn't install it. – axiac Mar 10 '16 at 12:06

3 Answers3

2

I finally found a solution to the problem. Sample: https://3v4l.org/jk7BS

function myErrorHandler($errno, $errstr, $errfile, $errline) {
    if (!!$errstr) {
        throw new Exception($errstr);
    }
    return false;
}

set_error_handler('myErrorHandler');

After writing this code, you can control, such as exceptions in Java and other languages.

function myErrorHandler($errno, $errstr, $errfile, $errline) {
    if (!!$errstr) {
        throw new Exception($errstr);
    }
    return false;
}

set_error_handler('myErrorHandler');

try{
    //division by zero
    //$number = 5/0;
    unlink("c:\boot.bat");
    $a=5+3;
    echo $a;
}
    catch(Exception $ex){
    echo  "Dotum bi hata yapmış olmalısın mesaj bu:".$ex->getMessage(), ' : Hadi kolay gelsin sana canım'; 
}
cArf
  • 81
  • 1
  • 5
  • Indeed, that's the way to do it. This is also suggested by the PHP [documentation](http://php.net/manual/en/class.errorexception.php#errorexception.examples). PHP 7 changed into exceptions some of the warnings and errors it used to trigger on previous version. Let's hope they will continue this trend and sometime in the future we won't need to write code to convert the errors to exceptions. – axiac Mar 11 '16 at 10:19
1

First, the code you posted works the same on both PHP 5 and PHP 7. It didn't use to throw an exception on PHP 5; this functionality (throw exceptions instead of triggering runtime errors and warnings) was introduced in PHP 7.

You could in PHP 5 (and you still can in PHP 7) install an error handler that converts the errors to exceptions and throws them. There already are several links (in other answers and comments) to answers where this technique is displayed.

The documentation of the new class DivisionByZeroError (introduced in PHP 7) explains:

DivisionByZeroError is thrown when an attempt is made to divide a number by zero.

Unfortunately, it seems the exception is thrown only on $x % 0 and intdiv($x, 0). The division by zero using the regular division operator $x / 0 doesn't throw the exception, it still works the same way it worked on PHP 5 (i.e. triggers a warning).

There is a bug #71306 reported for this situation on the PHP support site but it was officially classified as "not a bug" because the division operator / produces a floating point number and the floating point representation provides support for ±INF or NaN.

In conclusion, in PHP 7, the division by zero using / triggers a warning and returns INF or -INF. Only the modulo operator (%) and the integer division (function intdiv()) throw DivisionByZeroError when the divider is zero.

axiac
  • 68,258
  • 9
  • 99
  • 134
-1

Dividing by zero returns a warning and warnings cannot be caught with exception handlers. Please see https://stackoverflow.com/a/6040555/1441858.

Apologies, I now see that PHP7 throws an error on division by zero.

Community
  • 1
  • 1
Egg
  • 1,782
  • 1
  • 12
  • 28
  • The answer you linked to was written 5 years ago. It definitely doesn't cover PHP 7. – axiac Mar 10 '16 at 11:47
  • Sorry, you're right. [PHP7 throws an error](http://php.net/manual/en/class.divisionbyzeroerror.php) on division by zero. – Egg Mar 10 '16 at 11:53
  • Unfortunately it is not that simple. The documentation is misleading :-( Read [my answer](http://stackoverflow.com/a/35915946/4265352) for details. – axiac Mar 10 '16 at 12:03
  • This is useless. Let us admit. a fiasco in PHP exceptions. Even a fully php fiasko. I do not know why I'm still trying to learn php. – cArf Mar 10 '16 at 12:30