PHP7:Zerro Error的Catch Division

时间:2016-12-05 09:17:26

标签: php error-handling php-7

是否可以通过PHP7中的Zerro错误捕获分区?现在我收到警告。

try {
    $a = 1 / 0;
} catch (Error $error) {
    echo $error->getMessage();
}

1 个答案:

答案 0 :(得分:0)

<?php
$a=10;
$b=0;


// for php 5 
try{

    if ($b==0.0) {
    throw new Exception("Devide By Zero Exception");

    }else{
        $c = $a / $b;
    }
}
catch (Exception $error) {
    echo $error->getMessage();
}

//  for php7

try{
    $c=$a/$b;
}
catch(Throwable $t){
    printf("Type: %s<br>", get_class($t));
    printf("Message: %s<br>", $t->getMessage());
}
?>