Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

cvs: phpdoc /en/language/oop5 exceptions.xml
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/....5&t
y=u
Index: phpdoc/en/language/oop5/exceptions.xml
diff -u phpdoc/en/language/oop5/exceptions.xml:1.4 phpdoc/en/language/oop5/e
xceptions.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 c
ode
+    protected $file;                            // source filename of excep
tion
+    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 backtrac
e()
+    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 disp
lay
}
?>
]]>
@@ -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_CUSTO
M);
-}
-catch (MyException $e) {            /* Will be caught */
-  echo "Caught my exception\n", $e;
-  $e->customFunction();
+    $o = new  TestException(TestException::THROW_CUSTO
M);
+} 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_DEFAU
LT);
-}
-catch (MyException $e) {            /* Doesn't match this type */
-  echo "Caught my exception\n", $e;
-  $e->customFunction();
+    $o = new  TestException(TestException::THROW_DEFAU
LT);
+} 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_CUSTO
M);
+    $o = new  TestException(TestException::THROW_CUSTO
M);
+} 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>

Report this thread to moderator Post Follow-up to this message
Old Post
Aidan Lister
09-30-04 01:04 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PHP Documentation archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:39 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.