Matthew Weier O'Phinney wrote:

* Curt Zirzow <[EMAIL PROTECTED]>:

* Thus wrote Matthew Weier O'Phinney:

The problem I'm running into: what do I pass as arguments to catch()?
The articles on ZE2 use something like: catch (Exception $e) {}, or
something like catch(MyException $e) (where MyException is a class they
defined in their examples). Is the 'Exception' class a base
class/handler with PHP5? Do I need to create my own exception handler
classes? Do I even need to catch objects of a specific type, or can I
simply do:
   catch ($error) {
       do something with $error
   }

At minimum you should always at least catch the Exception class:

catch (Exception $e) { }


So, the Exception class is in the PHP5 distribution, then? Do I need to
include/require it, or is it implicit in simply running PHP5?

<snip>

class foo {
 function myException() {
   throw new MyException('Exception thrown');
 }
 function standardException() {
   throw new Exception();
 }
}

$f = new foo();

try {
 $f->myException();
}
catch (MyException $e) {
 echo "Caught my exception\n", $e;
 $e->customFunction();
}
catch (Exception $e) {
 echo "Default Exception caught\n", $e;
}

try {
 $f->standardException();
}
catch (MyException $e) {
 echo "Caught my exception\n", $e;
 $e->customFunction();
}
catch (Exception $e) {
 echo "Default Exception caught\n", $e;
}


Next question: do I have to 'throw' an error for it to be caught? Again,
coming from perl, if I try to eval something and it fails, I don't have
to throw in error -- if one occurs, I catch it with the 'if ($@)'
construct. Is 'catch (Exception $e)' equivalent? i.e., if an error
occurs in a try block that isn't specifically thrown, will that
construct catch it?

evals don't throw errors as far as I know, unless you throw it yourself from within the eval. The few internal functions that do throw exceptions can be caught using the catch(Exception $e) method, if you want to MAKE something throw an exception, then you need to explicitly tell it to THROW. Remember though, this is not an error-mechanism! It's exceptions... Errors are returned the standard way, and can be handled using error_handlers (http://www.php.net/manual/en/ref.errorfunc.php)

Hope that helped,
- Tul

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to