How do you use Exceptions?

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Gambler
Forum Contributor
Posts: 246
Joined: Thu Dec 08, 2005 7:10 pm

How do you use Exceptions?

Post by Gambler »

Do you use inheritance or error codes? Do you create separate file for each exception class?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

inheritance.. yes.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Do you create separate file for each exception class?
Typically yes, it keeps things much, much cleaner IMO.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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');
      }
   }
}
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Thanks Jcart. Can you show me what's inside exception.php?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

I don't use exception at all :) I'm still in PHP4 boat :)
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Then move on :P It took me a while too until I made the move, but now I'm happy with it :D
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Unfortunately I have to support huge, slowly dying system written in php4 (register_globals, magic_quotes_gpc, all that crap) =/.
Gambler
Forum Contributor
Posts: 246
Joined: Thu Dec 08, 2005 7:10 pm

Post by Gambler »

Code: Select all

<?php class MyException extends Exception{} ?>
Isn't it a bit empty for a separate file?
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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 8)
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

I see. Thanks a lot feyd 8)
bg
Forum Contributor
Posts: 157
Joined: Fri Sep 12, 2003 11:01 am

Post 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.
Post Reply