Example
- try {
- code
+ <?php
+ class MyException {
+ function MyException($_error) {
+ $this->error = $_error;
+ }
+
+ function getException() {
+ return $this->error;
+ }
+ }
- if (failure) {
- throw new MyException(Failure);
+ function ThrowException() {
+ throw new MyException("'This is an exception!'");
}
- code
- } catch ($exception) {
- handle exception
+ try {
+ } catch ($exception) {
+ print "There was an exception: " . $exception->getException();
+ print "\n";
+ }
- throw $exception; // Re-throw exception.
- }
+ try {
+ ThrowException();
+ } catch ($exception) {
+ print "There was an exception: " . $exception->getException();
+ print "\n";
+ }
+ ?>
- Old code that does not use exceptions will run without
- modifications.
+ Old code that does not define user-space functions 'catch',
+ 'throw' and 'try' will run without modifications.
* Namespaces.