repl.it
@jonathanbell/

Basic exception in PHP

PHP (Legacy)

No description

fork
loading
main.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function iWillError($a) {
  try {
    $z = $a / 3; // can't divide 3 by false
    if (!$z) {
      // The string provided here will be accessable via
      // Exception->getMessage();
      throw new Exception("You cannot divide a number by a boolean!"); 
    }
  } catch (Exception $e) {
    echo 'My exception: '.$e->getMessage(); 
    // Alternatly, you can throw the entire exception.
    //throw $e; 
  }
}

iWillError(false);
PHP 7.0.8 (Legacy: use the following for new features: Command-line PHP: https://repl.it/languages/php_cli PHP Web Server: https://repl.it/languages/php7