pear:rfc:pear2_exception_policy
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
pear:rfc:pear2_exception_policy [2009/09/22 16:52] – add global namespace character ashnazg | pear:rfc:pear2_exception_policy [2017/09/22 13:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 3: | Line 3: | ||
==== Document Information ==== | ==== Document Information ==== | ||
* Title: PEAR2 Exception Policy | * Title: PEAR2 Exception Policy | ||
- | * Version: 1.0.0 | + | * Version: 1.0.1 |
* Status: Ratified | * Status: Ratified | ||
* Type: Informative Document | * Type: Informative Document | ||
* Wiki link: http:// | * Wiki link: http:// | ||
- | * Last updated: | + | * Last updated: |
* Passed: | * Passed: | ||
Line 98: | Line 98: | ||
Q. **Why a base package exception interface? | Q. **Why a base package exception interface? | ||
- | A. By implementing the base package exception interface, all exceptions for a given pear package can be caught with '' | + | A. By implementing the base package exception interface, all exceptions for a given pear package can be caught with '' |
This allows end users to easily do the following: | This allows end users to easily do the following: | ||
Line 105: | Line 105: | ||
try { | try { | ||
//do something with PEAR2\Package1 | //do something with PEAR2\Package1 | ||
- | } catch (PEAR2\Package1\Exception $e) { | + | } catch (\PEAR2\Package1\Exception $e) { |
//do the same thing with PEAR2\Package2 | //do the same thing with PEAR2\Package2 | ||
- | } catch(Exception $e) { | + | } catch(\Exception $e) { |
//an unknown exception occurred | //an unknown exception occurred | ||
} | } | ||
</ | </ | ||
+ | |||
+ | ===== Full Code Example ===== | ||
+ | This example contrasts the usage of a base Exception interface with a base Exception class. | ||
+ | |||
+ | Foo package that uses a base Exception interface: | ||
+ | < | ||
+ | <?php | ||
+ | namespace foo; | ||
+ | |||
+ | interface Exception {} | ||
+ | class InvalidArgumentException extends \InvalidArgumentException implements Exception {} | ||
+ | class OutOfBoundsException | ||
+ | |||
+ | class Foo { | ||
+ | |||
+ | public function badArg() { | ||
+ | throw new InvalidArgumentException(); | ||
+ | } | ||
+ | |||
+ | public function badBounds() { | ||
+ | throw new OutOfBoundsException(); | ||
+ | } | ||
+ | } | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | Bar package that uses a base Exception class: | ||
+ | < | ||
+ | <?php | ||
+ | namespace bar; | ||
+ | |||
+ | class Exception | ||
+ | class InvalidArgumentException extends \InvalidArgumentException {} | ||
+ | class OutOfBoundsException | ||
+ | |||
+ | class Bar { | ||
+ | |||
+ | public function badArg() { | ||
+ | throw new InvalidArgumentException(); | ||
+ | } | ||
+ | |||
+ | public function badBounds() { | ||
+ | throw new OutOfBoundsException(); | ||
+ | } | ||
+ | } | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | Test script: | ||
+ | < | ||
+ | <?php | ||
+ | |||
+ | echo ' | ||
+ | require_once ' | ||
+ | $x = new \foo\Foo(); | ||
+ | try { | ||
+ | echo ' | ||
+ | $x-> | ||
+ | } catch (\foo\Exception $e) { | ||
+ | echo ' | ||
+ | } | ||
+ | echo PHP_EOL; | ||
+ | |||
+ | echo ' | ||
+ | require_once ' | ||
+ | $y = new \bar\Bar(); | ||
+ | try { | ||
+ | echo ' | ||
+ | $y-> | ||
+ | } catch (\bar\Exception $e) { | ||
+ | echo ' | ||
+ | } | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | Test results: | ||
+ | < | ||
+ | Testing the Exception base INTERFACE ====================== | ||
+ | * Will catch(\foo\Exception) catch a \foo\InvalidArgumentException ??? | ||
+ | * Caught a \foo\Exception ok, so YES... as expected. | ||
+ | |||
+ | Testing the Exception base CLASS ===================== | ||
+ | * Will catch(\bar\Exception) catch a \bar\InvalidArgumentException ??? | ||
+ | PHP Fatal error: | ||
+ | Stack trace: | ||
+ | #0 / | ||
+ | #1 {main} | ||
+ | thrown in / | ||
+ | </ |
pear/rfc/pear2_exception_policy.1253638340.txt.gz · Last modified: 2017/09/22 13:28 (external edit)