mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-16 00:48:54 +00:00
Reworked examples
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@169557 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
parent
d436678a25
commit
f175b914ca
1 changed files with 108 additions and 108 deletions
|
@ -1,5 +1,5 @@
|
|||
<?xml version="1.0" encoding="iso-8859-1"?>
|
||||
<!-- $Revision: 1.4 $ -->
|
||||
<!-- $Revision: 1.5 $ -->
|
||||
<sect1 id="language.oop5.exceptions">
|
||||
<title>Exceptions</title>
|
||||
|
||||
|
@ -23,17 +23,16 @@
|
|||
<![CDATA[
|
||||
<?php
|
||||
try {
|
||||
$error = 'Always throw this error';
|
||||
throw new Exception($error);
|
||||
$error = 'Always throw this error';
|
||||
throw new Exception($error);
|
||||
|
||||
// code following an exception isn't executed.
|
||||
echo 'Never executed';
|
||||
}
|
||||
catch (Exception $e) {
|
||||
echo "Caught exception: ", $e, "\n";
|
||||
// code following an exception isn't executed.
|
||||
echo 'Never executed';
|
||||
} catch (Exception $e) {
|
||||
echo "Caught exception: ", $e, "\n";
|
||||
}
|
||||
|
||||
/* continue execution */
|
||||
// Continue execution
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
|
@ -52,23 +51,21 @@ catch (Exception $e) {
|
|||
<![CDATA[
|
||||
<?php
|
||||
class Exception {
|
||||
protected $message = 'Unknown exception'; // exception message
|
||||
protected $code = 0; // user defined exception code
|
||||
protected $file; // source filename of exception
|
||||
protected $line; // source line of exception
|
||||
|
||||
protected $message = 'Unknown exception'; // exception message
|
||||
protected $code = 0; // user defined exception code
|
||||
protected $file; // source filename of exception
|
||||
protected $line; // source line of exception
|
||||
function __construct($message = null, $code = 0);
|
||||
|
||||
function __construct(string $message=NULL, int code=0);
|
||||
final function getMessage(); // message of exception
|
||||
final function getCode(); // code of exception
|
||||
final function getFile(); // source filename
|
||||
final function getTrace(); // an array of the backtrace()
|
||||
final function getTraceAsString(); // formated string of trace
|
||||
|
||||
final function getMessage(); // message of exception
|
||||
final function getCode(); // code of exception
|
||||
final function getFile(); // source filename
|
||||
final function getTrace(); // an array of the backtrace()
|
||||
final function getTraceAsString(); // formated string of trace
|
||||
|
||||
/* Overrideable */
|
||||
function _toString(); // formated string for display
|
||||
|
||||
/* Overrideable */
|
||||
function _toString(); // formated string for display
|
||||
}
|
||||
?>
|
||||
]]>
|
||||
|
@ -88,112 +85,115 @@ class Exception {
|
|||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Define a custom exception class
|
||||
*/
|
||||
class MyException extends Exception {
|
||||
|
||||
/* Redefine the exception so message isn't optional */
|
||||
public function __construct($message, $code = 0) {
|
||||
|
||||
// custom stuff you want to do..
|
||||
// ...
|
||||
// Redefine the exception so message isn't optional
|
||||
public function __construct($message, $code = 0) {
|
||||
// some code
|
||||
|
||||
/* make sure everything is assigned properly */
|
||||
parent::__construct($message, $code);
|
||||
}
|
||||
|
||||
/* 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";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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 isnt allowed as a parameter', 6);
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
// No exception, object will be created.
|
||||
$this->var = $avalue;
|
||||
break;
|
||||
|
||||
// make sure everything is assigned properly
|
||||
parent::__construct($message, $code);
|
||||
}
|
||||
|
||||
}
|
||||
// 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";
|
||||
}
|
||||
}
|
||||
|
||||
$o = null;
|
||||
|
||||
/**
|
||||
* 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 isnt 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);
|
||||
$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;
|
||||
}
|
||||
catch (MyException $e) { /* Will be caught */
|
||||
echo "Caught my exception\n", $e;
|
||||
$e->customFunction();
|
||||
}
|
||||
catch (Exception $e) { /* Skipped */
|
||||
echo "Caught Default Exception\n", $e;
|
||||
}
|
||||
var_dump($o); /* continue execution */
|
||||
echo "\n\n";
|
||||
|
||||
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;
|
||||
}
|
||||
var_dump($o); /* continue execution */
|
||||
// Continue execution
|
||||
var_dump($o);
|
||||
echo "\n\n";
|
||||
|
||||
|
||||
// Example 2
|
||||
try {
|
||||
$o = new TestException(TestException::THROW_CUSTOM);
|
||||
$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;
|
||||
}
|
||||
catch (Exception $e) { /* Will be caught */
|
||||
echo "Default Exception caught\n", $e;
|
||||
}
|
||||
var_dump($o); /* continue execution */
|
||||
echo "\n\n";
|
||||
|
||||
try {
|
||||
$o = new TestException();
|
||||
}
|
||||
catch (Exception $e) { /* skipped, no exception */
|
||||
echo "Default Exception caught\n", $e;
|
||||
}
|
||||
var_dump($o); /* continue execution */
|
||||
// Continue execution
|
||||
var_dump($o);
|
||||
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);
|
||||
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);
|
||||
echo "\n\n";
|
||||
]]>
|
||||
</programlisting>
|
||||
</example>
|
||||
|
|
Loading…
Reference in a new issue