DivisionByZeroError
(PHP 7, PHP 8)
Введение
DivisionByZeroError выбрасывается при попытке поделить число на ноль.
Обзор классов
DivisionByZeroError implements Error {
/* Наследуемые свойства */
protected string $message ;
protected int $code ;
protected string $file ;
protected int $line ;
/* Наследуемые методы */
final public Error::getMessage ( ) : string
final public Error::getPrevious ( ) : Throwable|null
final public Error::getCode ( ) : mixed
final public Error::getFile ( ) : string
final public Error::getLine ( ) : int
final public Error::getTrace ( ) : array
final public Error::getTraceAsString ( ) : string
public Error::__toString ( ) : string
final private Error::__clone ( ) : void
}
// 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";
}
2
8ctopus ¶5 months ago
try {
echo intdiv(2, 0);
} catch (DivisionByZeroError $e) {
echo "Caught DivisionByZeroError!\n";
}
try {
echo (2 / 0);
} catch (DivisionByZeroError $e) {
echo "Caught DivisionByZeroError!\n";
}
1
Alex ¶2 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.
-28
Manjunath ¶5 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();
Смотрите также:
Описание на ru2.php.net
Описание на php.ru