aidan Wed Sep 29 11:46:16 2004 EDT
Modified files:
/phpdoc/en/language/oop5 exceptions.xml
Log:
Reworked examples
http://cvs.php.net/diff.php/phpdoc/en/language/oop5/exceptions.xml?r1=1.4&r2=1.5&ty=u
Index: phpdoc/en/language/oop5/exceptions.xml
diff -u phpdoc/en/language/oop5/exceptions.xml:1.4
phpdoc/en/language/oop5/exceptions.xml:1.5
--- phpdoc/en/language/oop5/exceptions.xml:1.4 Thu Aug 26 01:40:29 2004
+++ phpdoc/en/language/oop5/exceptions.xml Wed Sep 29 11:46:16 2004
@@ -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 @@
<![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
+
+ function __construct($message = null, $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
- 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(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
-
- /* Overrideable */
- function _toString(); // formated string for display
-
+ /* Overrideable */
+ function _toString(); // formated string for display
}
?>
]]>
@@ -88,112 +85,115 @@
<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;
-
+ // make sure everything is assigned properly
+ parent::__construct($message, $code);
+ }
- function __construct($avalue = self::THROW_NONE) {
+ // custom string representation of object */
+ public function __toString() {
+ return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
+ }
- switch ($avalue) {
- case self::THROW_CUSTOM:
- // throw custom exception
- throw new MyException('1 is an invalid parameter', 5);
- break;
+ public function customFunction() {
+ echo "A Custom function for this type of exception\n";
+ }
+}
- case self::THROW_DEFAULT:
- // throw default one.
- throw new Exception('2 isnt allowed as a parameter', 6);
- break;
+/**
+ * Create a class to test the exception
+ */
+class TestException {
- default:
- // No exception, object will be created.
- $this->var = $avalue;
- break;
+ 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;
+ }
}
-
- }
-
}
-$o = null;
+// Example 1
try {
- $o = new TestException(TestException::THROW_CUSTOM);
-}
-catch (MyException $e) { /* Will be caught */
- echo "Caught my exception\n", $e;
- $e->customFunction();
+ $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 (Exception $e) { /* Skipped */
- 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_DEFAULT);
-}
-catch (MyException $e) { /* Doesn't match this type */
- echo "Caught my exception\n", $e;
- $e->customFunction();
+ $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 "Caught Default Exception\n", $e;
-}
-var_dump($o); /* continue execution */
+
+// Continue execution
+var_dump($o);
echo "\n\n";
+// Example 3
try {
- $o = new TestException(TestException::THROW_CUSTOM);
+ $o = new TestException(TestException::THROW_CUSTOM);
+} catch (Exception $e) { // Will be caught
+ echo "Default Exception caught\n", $e;
}
-catch (Exception $e) { /* Will be caught */
- echo "Default Exception caught\n", $e;
-}
-var_dump($o); /* continue execution */
+
+// 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;
+ $o = new TestException();
+} catch (Exception $e) { // Skipped, no exception
+ echo "Default Exception caught\n", $e;
}
-var_dump($o); /* continue execution */
-echo "\n\n";
-
+// Continue execution
+var_dump($o);
+echo "\n\n";
]]>
</programlisting>
</example>