Page 1 of 2
How do you use Exceptions?
Posted: Wed Jun 21, 2006 2:20 pm
by Gambler
Do you use inheritance or error codes? Do you create separate file for each exception class?
Posted: Wed Jun 21, 2006 2:31 pm
by feyd
inheritance.. yes.
Posted: Wed Jun 21, 2006 2:32 pm
by John Cartwright
Do you create separate file for each exception class?
Typically yes, it keeps things much, much cleaner IMO.
Posted: Wed Jun 21, 2006 3:23 pm
by Oren
Sounds interesting... What do you mean a class for exceptions?
For me, the word 'exceptions' in PHP means one thing:
Code: Select all
if (some_error_occurs)
{
throw new exception('Error message');
}
Is there more than that?
Posted: Wed Jun 21, 2006 3:29 pm
by John Cartwright
Code: Select all
require_once 'config/exception.php';
class App_Configuration
{
public function __construct()
{
if ($someCondition)
{
throw new App_Configuration_Exception('Error Message');
}
}
}
Posted: Wed Jun 21, 2006 3:42 pm
by Oren
Thanks Jcart. Can you show me what's inside exception.php?
Posted: Wed Jun 21, 2006 4:49 pm
by Weirdan
I don't use exception at all

I'm still in PHP4 boat

Posted: Wed Jun 21, 2006 4:52 pm
by Oren
Then move on

It took me a while too until I made the move, but now I'm happy with it

Posted: Wed Jun 21, 2006 4:56 pm
by Weirdan
Unfortunately I have to support huge, slowly dying system written in php4 (register_globals, magic_quotes_gpc, all that crap) =/.
Posted: Wed Jun 21, 2006 9:25 pm
by Gambler
Code: Select all
<?php class MyException extends Exception{} ?>
Isn't it a bit empty for a separate file?
Posted: Thu Jun 22, 2006 2:21 am
by Oren
Oh thanks
Gambler. Now I remember I've read about it when I first read about exceptions on the Zend site.
Thanks alot guys

Posted: Thu Jun 22, 2006 3:23 pm
by Oren
So why this:
Code: Select all
class MyException extends Exception{}
is better than this:
Code: Select all
throw new exception('Error message', 1);
Why not using error codes like in the above example?
Posted: Thu Jun 22, 2006 3:25 pm
by feyd
class names are more portable and easier to handle (consistency among other things) .. there's also having localized text be handled by the output based on what the object it gets instead of having to interrogate its innards to figure out what it got, let alone what to do with it.
Posted: Thu Jun 22, 2006 3:27 pm
by Oren
I see. Thanks a lot
feyd 
Posted: Thu Jun 22, 2006 6:57 pm
by bg
can someone post a practical use of an exception, like a code example? I'm looking at the example given on php's site and its not helping me too much to understand whats going on. Can you throw exceptions without a try block? is it useful, if so, when? I appreciate any help in advance.