WEB DEVELOPER SITE
PYTHON
 

PHP 7 - Error Handling




PHP 7 error handling and reporting has been changed


If there are no matching blocks, then a default exception handler installed with set_exception_handler() will be called.

PHP 7- Error Handling example



 
<?php
   class MathOperations {
      protected $n = 7;

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

   $mathOperationsObj = new MathOperations();
   print($mathOperationsObj->doOperation());
?>

result
Modulo by zero