Example of PHP Exception - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of Example of PHP Exception.php

  1. <?php
  2. function inverse($x) {
  3.     if (!$x) {
  4.         throw new Exception('Division by zero.');
  5.     }
  6.     return 1/$x;
  7. }
  8.  
  9. try {
  10.     echo inverse(5) . "\n";
  11.     echo inverse(0) . "\n";
  12. } catch (Exception $e) {
  13.     echo 'Caught exception: ',  $e->getMessage(), "\n";
  14. }
  15.  
  16. // Continue execution
  17. echo "Hello World\n";
File Description
  • Example of PHP Exception
  • PHP Code
  • 08 Aug-2018
  • 309 Bytes
You can Share it: