@ 2013-09-20T21:01:23Z <?php
class Exception
{
protected $message = 'Unknown exception'; // exception message
private $string; // __toString cache
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exception
private $trace; // backtrace
private $previous; // previous exception if nested exception
public function __construct($message = null, $code = 0, Exception $previous = null){}
// Overrideable
public function __toString(); // formatted string for display
}
/**
* Define a custom exception class
*/
class MyException extends Exception
{tick_handler();
// Redefine the exception so message isn't optional
public function __construct($message, $code = 0, Exception $previous = null) {
// some code
// make sure everything is assigned properly
parent::__construct($message, $code, $previous);
}
// custom string representation of object
public function __toString() {
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}
public function customFunction() {
echo "A custom function for this type of exception\n";
}
}
/**
* Create a class to test the exception
*/
class TestException
{
public $var;
const THROW_NONE = 0;
const THROW_CUSTOM = 1;
const THROW_DEFAULT = 2;
function __construct($avalue = self::THROW_NONE) {
switch ($avalue) {
case self::THROW_CUSTOM:
// throw custom exception
throw new MyException('1 is an invalid parameter', 5);
break;
case self::THROW_DEFAULT:
// throw default one.
throw new Exception('2 is not allowed as a parameter', 6);
break;
default:
// No exception, object will be created.
$this->var = $avalue;
break;
}
}
}
// Example 1
try {
$o = new TestException(TestException::THROW_CUSTOM);
} catch (MyException $e) { // Will be caught
echo "Caught my exception\n", $e;
$e->customFunction();
} catch (Exception $e) { // Skipped
echo "Caught Default Exception\n", $e;
}
// Continue execution
var_dump($o); // Null
echo "\n\n";
// Example 2
try {
$o = new TestException(TestException::THROW_DEFAULT);
} catch (MyException $e) { // Doesn't match this type
echo "Caught my exception\n", $e;
$e->customFunction();
} catch (Exception $e) { // Will be caught
echo "Caught Default Exception\n", $e;
}
// Continue execution
var_dump($o); // Null
echo "\n\n";
// Example 3
try {
$o = new TestException(TestException::THROW_CUSTOM);
} catch (Exception $e) { // Will be caught
echo "Default Exception caught\n", $e;
}
// Continue execution
var_dump($o); // Null
echo "\n\n";
// Example 4
try {
$o = new TestException();
} catch (Exception $e) { // Skipped, no exception
echo "Default Exception caught\n", $e;
}
// Continue execution
var_dump($o); // TestException
echo "\n\n";
?>
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 5.4.20 0.010 0.053 12.39 5.4.19 0.014 0.067 12.38 5.4.18 0.023 0.062 12.38 5.4.17 0.015 0.058 12.39 5.4.16 0.014 0.056 12.39 5.4.15 0.011 0.057 12.39 5.4.14 0.013 0.059 12.07 5.4.13 0.013 0.055 12.06 5.4.12 0.013 0.071 12.01 5.4.11 0.018 0.056 12.01 5.4.10 0.017 0.052 12.01 5.4.9 0.014 0.061 12.01 5.4.8 0.016 0.053 12.01 5.4.7 0.010 0.053 12.01 5.4.6 0.013 0.047 12.01 5.4.5 0.011 0.056 12.01 5.4.4 0.016 0.055 12.00 5.4.3 0.012 0.048 11.99 5.4.2 0.012 0.050 11.99 5.4.1 0.012 0.060 12.00 5.4.0 0.012 0.063 11.49 5.3.27 0.012 0.053 12.72 5.3.26 0.011 0.054 12.72 5.3.25 0.016 0.064 12.72 5.3.24 0.013 0.047 12.72 5.3.23 0.012 0.052 12.71 5.3.22 0.014 0.062 12.68 5.3.21 0.015 0.061 12.68 5.3.20 0.013 0.047 12.68 5.3.19 0.017 0.050 12.68 5.3.18 0.017 0.068 12.67 5.3.17 0.020 0.065 12.67 5.3.16 0.016 0.063 12.67 5.3.15 0.013 0.052 12.67 5.3.14 0.012 0.067 12.66 5.3.13 0.011 0.055 12.66 5.3.12 0.014 0.061 12.66 5.3.11 0.015 0.056 12.66 5.3.10 0.016 0.051 12.13 5.3.9 0.024 0.073 12.10 5.3.8 0.016 0.058 12.09 5.3.7 0.014 0.057 12.09 5.3.6 0.017 0.068 12.07 5.3.5 0.011 0.052 12.02 5.3.4 0.015 0.057 12.02 5.3.3 0.024 0.057 11.98 5.3.2 0.012 0.057 11.76 5.3.1 0.016 0.050 11.73 5.3.0 0.014 0.047 11.70
preferences:dark mode live preview
141.53 ms | 1394 KiB | 7 Q