From f175b914ca685ce3f376f0af8d0c6120fe6ef4ac Mon Sep 17 00:00:00 2001 From: Aidan Lister Date: Wed, 29 Sep 2004 15:46:16 +0000 Subject: [PATCH] Reworked examples git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@169557 c90b9560-bf6c-de11-be94-00142212c4b1 --- language/oop5/exceptions.xml | 216 +++++++++++++++++------------------ 1 file changed, 108 insertions(+), 108 deletions(-) diff --git a/language/oop5/exceptions.xml b/language/oop5/exceptions.xml index 8ecaa4661a..08c8b3b624 100644 --- a/language/oop5/exceptions.xml +++ b/language/oop5/exceptions.xml @@ -1,5 +1,5 @@ - + Exceptions @@ -23,17 +23,16 @@ ]]> @@ -52,23 +51,21 @@ catch (Exception $e) { ]]> @@ -88,112 +85,115 @@ class Exception { 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"; ]]>