Re: PHP5 exception handling
| From: | Tularis | Date: | Fri, 30 Jul 2004 19:23:44 +0000 |
| Subject: | Re: PHP5 exception handling | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-192343@lists.php.net to get a copy of this message | ||
Matthew Weier O'Phinney wrote:
* Curt Zirzow <php-general@zirzow.dyndns.org>:Hope that helped, - Tul* Thus wrote Matthew Weier O'Phinney: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>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) {At minimum you should always at least catch the Exception class: catch (Exception $e) { }do something with $error}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)