略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: DivisionByZeroError

2024-11-21

DivisionByZeroError

(PHP 7, PHP 8)

简介

DivisionByZeroError 当除数为零时被抛出。

类摘要

class DivisionByZeroError extends ArithmeticError {
/* 继承的属性 */
protected string $message = "";
private string $string = "";
protected int $code;
protected string $file = "";
protected int $line;
private array $trace = [];
private ?Throwable $previous = null;
/* 继承的方法 */
final public Error::getMessage(): string
final public Error::getPrevious(): ?Throwable
final public Error::getCode(): int
final public Error::getFile(): string
final public Error::getLine(): int
final public Error::getTrace(): array
final public Error::getTraceAsString(): string
public Error::__toString(): string
private Error::__clone(): void
}
add a noteadd a note

User Contributed Notes 4 notes

up
3
8ctopus
1 year ago
Use of arithmetic operator / does not throw an exception in php 7, while it does in php 8.

<?php

try {
    echo
intdiv(2, 0);
} catch (
DivisionByZeroError $e) {
    echo
"Caught DivisionByZeroError!\n";
}

try {
    echo (
2 / 0);
} catch (
DivisionByZeroError $e) {
    echo
"Caught DivisionByZeroError!\n";
}
?>

# php 7
$ php test.php
caught division by zero for intdiv()
PHP Warning:  Division by zero in test.php on line 10
PHP Stack trace:
PHP   1. {main}() test.php:0

Warning: Division by zero in test.php on line 10

Call Stack:
    0.0740     417272   1. {main}() test.php:0

# php 8
$ php test.php

caught division by zero for intdiv()
caught division by zero for /
up
5
salsi at icosaedro dot it
6 years ago
Note that on division by zero 1/0 and module by zero 1%0 an E_WARNING is triggered first (probably for backward compatibility with PHP5), then the DivisionByZeroError exception is thrown next.

The result is, for example, that if you set the maximum level of error detection with error_level(-1) and you also map errors to exception, say ErrorException, then on division by zero only this latter exception is thrown reporting "Division by zero". The result is that a code like this:

<?php
// Set a safe environment:
error_reporting(-1);

// Maps errors to ErrorException.
function my_error_handler($errno, $message)
{ throw new
ErrorException($message); }

try {
    echo
1/0;
}
catch(
ErrorException $e){
    echo
"got $e";
}
?>

allows to detect such error in the same way under PHP5 and PHP7, although the DivisionByZeroError exception is masked off by ErrorException.
up
-2
Alex
3 years ago
This error is thrown only for integer division - this is when using "intdiv" function or "%" operator. In all cases you will get an E_WARNING when dividing by zero.
up
-39
Manjunath
6 years ago
<?php
class MathOperation extends Error
{
    protected
$n = 10;

   
// Try to get the Division by Zero error object and display as Exception
   
public function doArithmeticOperation(): string
   
{
        try {
           
$value = $this->n % 0;
        } catch (
DivisionByZeroError $e) {
            return
$e->getMessage();
        }
    }
}

$mathOperationObj = new MathOperation();
echo
$mathOperationObj->doArithmeticOperation();

官方地址:https://www.php.net/manual/en/class.divisionbyzeroerror.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3