[PHP] PHP5 Exception Handling
Just bouncing a thought on you. I currently have a default exception handler setup. So far in the code, I throw an exception when something is wrong. i.e. Missing file, invalid argument, failed db connection etc... My thought was centering around if I still needed to use try/catch in the code, since I have a default exception handler. Normally, I would rethrow exceptions for it to bubble back up to the top, where its caught, and something useful done with the stack. So I removed all try/catch blocks, and its seems to be working as it should be. Just wondering if there may be an gotchas with this chain of thought. Or should I keep the try/catch blocks as it is??? Thanks -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP5 Exception Handling
Gerard Samuel wrote: Just bouncing a thought on you. I currently have a default exception handler setup. So far in the code, I throw an exception when something is wrong. i.e. Missing file, invalid argument, failed db connection etc... My thought was centering around if I still needed to use try/catch in the code, since I have a default exception handler. Normally, I would rethrow exceptions for it to bubble back up to the top, where its caught, and something useful done with the stack. So I removed all try/catch blocks, and its seems to be working as it should be. Just wondering if there may be an gotchas with this chain of thought. Or should I keep the try/catch blocks as it is??? I think you should leave them in. the idea behind the default exception handler is to catch uncaught exceptions - uncaught exceptions should be the exception rather than the rule. also you don't have to rethrow exceptions, sometimes its very useful to just catch (possibly ignore) and continue execution just past the catch block. http://be.php.net/manual/en/function.set-exception-handler.php the gotcha tha manual states that execution stops after the exception handler in called, which is fairly limiting. /the gotcha Another way to look at it would be to specify that all 'pages' are based on the following: try { // global include // process some stuff // do output } catch (Exception $e) { // error - show 404 or something? } another thing about exceptions, you can be very specific about catching, also the catches can be 'stacked': try { } catch (DBException $e) { } catch (UnknownObject $e) { } catch (Exception $e) { } this implies that the code uses user defined Exception subclasses. i.e. you can write your own Exception classes that you can throw around :-) Thanks -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP5 Exception Handling
Jochem Maas wrote: Gerard Samuel wrote: Just bouncing a thought on you. I currently have a default exception handler setup. So far in the code, I throw an exception when something is wrong. i.e. Missing file, invalid argument, failed db connection etc... My thought was centering around if I still needed to use try/catch in the code, since I have a default exception handler. Normally, I would rethrow exceptions for it to bubble back up to the top, where its caught, and something useful done with the stack. So I removed all try/catch blocks, and its seems to be working as it should be. Just wondering if there may be an gotchas with this chain of thought. Or should I keep the try/catch blocks as it is??? I think you should leave them in. the idea behind the default exception handler is to catch uncaught exceptions - uncaught exceptions should be the exception rather than the rule. also you don't have to rethrow exceptions, sometimes its very useful to just catch (possibly ignore) and continue execution just past the catch block. http://be.php.net/manual/en/function.set-exception-handler.php the gotcha tha manual states that execution stops after the exception handler in called, which is fairly limiting. /the gotcha Another way to look at it would be to specify that all 'pages' are based on the following: try { // global include // process some stuff // do output } catch (Exception $e) { // error - show 404 or something? } another thing about exceptions, you can be very specific about catching, also the catches can be 'stacked': try { } catch (DBException $e) { } catch (UnknownObject $e) { } catch (Exception $e) { } this implies that the code uses user defined Exception subclasses. i.e. you can write your own Exception classes that you can throw around :-) I understand what you're getting at. Let me describe a little more of my setup. All requests go through the framework's index.php, where I used to have something like what you described - ?php try { // code for page } catch(Exception $e) { // Log exception // Display pretty error page with stack die; } ? The problem with this, is that its possible for the logging mechanism, to throw an exception, leaving the exception not logged, along with the default *ugly* page. So thats when I decided to start using the default exception handler. Currently, I have the default exception handler, log the exception, and display a *pretty* page. If the logging mechanism does throw an exception, the exception will not be logged, but at least, the *pretty* page does display. Your point about page execution stopping after the default exception handler is called, is acceptable for me. From my original email - So far in the code, I throw an exception when something is wrong. i.e. Missing file, invalid argument, failed db connection etc... What I mean by this is that if the code sees that there is no other way to go, to throw an exception. With the setup Im currently using, I should still be able to use exceptions to control execution flow, as long as they are tried/caught properly. As I understand it, exceptions can be used to a) Handle errors b) Control execution flow Right now, Im only concerned about a). Hopefully that makes my situation a bit clearer. So with this in mind, the default exception handler, handles the *pretty* error page, and tries to log the exception. And I remove all try/catch blocks from the framework code (the reason for this email). Then in the application, that runs on top of the framework, then I can use try/catch to control code execution. I hope that was clearer. If I've missed the point, feel free to bash me in my head ;). -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] PHP5 exception handling
I've done some reading on the ZE2 exception handling, but I'm not quite sure I understand how it works. I'm familiar with the following in perl: eval { do some stuff... } if ($@) { report an exception... } Which looks a lot like the following in PHP5: try { do some stuff... } catch () { } 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 } Thanks in advance. -- Matthew Weier O'Phinney | WEBSITES: Webmaster and IT Specialist | http://www.garden.org National Gardening Association| http://www.kidsgardening.com 802-863-5251 x156 | http://nationalgardenmonth.org mailto:[EMAIL PROTECTED] | http://vermontbotanical.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP5 exception handling
* 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) { } If the class instance your calling throws a custom exception definition you can attempt to catch that before the standard Exception class: ?php class MyException extends Exception { /* Redefine the exception so message isn't optional */ public function __construct($message, $code = 0) { // custom stuff you want to do.. // ... 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 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; } HTH, Curt -- First, let me assure you that this is not one of those shady pyramid schemes you've been hearing about. No, sir. Our model is the trapezoid! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP5 exception handling
You can check out how exceptions are handled in Java or C++ as well, they all sorta work the same, you may find more complete docs for them. Jason On 30 Jul 2004 14:58:59 -, Matthew Weier O'Phinney [EMAIL PROTECTED] wrote: I've done some reading on the ZE2 exception handling, but I'm not quite sure I understand how it works. I'm familiar with the following in perl: eval { do some stuff... } if ($@) { report an exception... } Which looks a lot like the following in PHP5: try { do some stuff... } catch () { } 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 } Thanks in advance. -- Matthew Weier O'Phinney | WEBSITES: Webmaster and IT Specialist | http://www.garden.org National Gardening Association| http://www.kidsgardening.com 802-863-5251 x156 | http://nationalgardenmonth.org mailto:[EMAIL PROTECTED] | http://vermontbotanical.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP5 exception handling
* 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? -- Matthew Weier O'Phinney | WEBSITES: Webmaster and IT Specialist | http://www.garden.org National Gardening Association| http://www.kidsgardening.com 802-863-5251 x156 | http://nationalgardenmonth.org mailto:[EMAIL PROTECTED] | http://vermontbotanical.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP5 exception handling
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
Re: [PHP] PHP5 exception handling
* Tularis [EMAIL PROTECTED]: 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 snip 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) That helps explain it a lot. Perl's eval mechanism is much more flexible, but also very non-standard from what I've been reading of C++ and Java (thanks to the person who recommended that reading). I was thinking of exceptions as a way to gracefully handle code errors, but it appears now that they are more a mechanism to handle return values that fall outside the expected realm. I'll stick to error_handlers for code errors, and start seeing what use I can make of the try{} catch(){} constructs. Off to the experimentation room... -- Matthew Weier O'Phinney | WEBSITES: Webmaster and IT Specialist | http://www.garden.org National Gardening Association| http://www.kidsgardening.com 802-863-5251 x156 | http://nationalgardenmonth.org mailto:[EMAIL PROTECTED] | http://vermontbotanical.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php