WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML


 

PHP 7 - Error Handling




From PHP 7, error handling and reporting has been changed. Rather than announcing errors through the conventional traditional error reporting mechanism utilized by PHP 5, presently most errors are handled by throwing Error exceptions. Like special cases, these Error exemptions rise until they achieve the primary coordinating catch square. On the off chance that there are no coordinating squares, at that point a default special case handler introduced with set_exception_handler() will be called. In the event that there is no default special case handler, at that point the exemption will be changed over to a lethal blunder and will be dealt with like a conventional error.

As the Error chain of importance isn't extended out from Exception, code that utilizes catch (Exception $e) { ... } blocks to handle with uncaught special cases in PHP 5 won't handle with such blunders. A catch (Error $e) { ... } block or a set_exception_handler() handler is required to handler with handle fatal errors.


 
<?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