AmandaF Posted May 8, 2009 Share Posted May 8, 2009 I'm working on my first serious attempt at creating a php class. (As I said in another thread a few weeks ago, I'm trying to make the transition from programming procedurally in ColdFusion to object-oriented programming in PHP). I know that I can extend the Exception class to make my own custom Exceptions, and was wondering if it's considered good practice to only do this when necessary, or to make all exceptions custom. For example, let's say I have two methods, setFoo and setBar, both of which throw errors if their arguments are false. Which of these would be considered better: class FooBar { public function setFoo($foo) { if(!$foo) { throw new Exception('Foo is false!'); } } public function setBar($bar) { if(!$bar) { throw new Exception('Bar is false!'); } } } class FooException extends Exception {} class BarException extends Exception {} class FooBar { public function setFoo($foo) { if(!$foo) { throw new FooException('Foo is false!'); } } public function setBar($bar) { if(!$bar) { throw new BarException('Bar is false!'); } } } On one hand, the first example seems better because I can use the methods without having to remember which type of exception to catch. On the other hand, the second one seems like it would be better because it makes it easier to determine which type of error occurred. So what's the standard way of doing this? Quote Link to comment https://forums.phpfreaks.com/topic/157364-when-to-use-custom-exceptions/ Share on other sites More sharing options...
JonnoTheDev Posted May 8, 2009 Share Posted May 8, 2009 You only need to extend if you want the program to perform an action if an exception is caught. For instance you may want the exception to be stored in a database, sent as an email or display a specific page to the user. There is no need to create a new extended class for every exception. Quote Link to comment https://forums.phpfreaks.com/topic/157364-when-to-use-custom-exceptions/#findComment-829474 Share on other sites More sharing options...
Axeia Posted May 8, 2009 Share Posted May 8, 2009 Throwing exceptions should be used for error handling, asserts can be used to make a little helper line during development and if you don't want something to simply happen an if will work fine. Quote Link to comment https://forums.phpfreaks.com/topic/157364-when-to-use-custom-exceptions/#findComment-829554 Share on other sites More sharing options...
AmandaF Posted May 8, 2009 Author Share Posted May 8, 2009 Neil, thanks. Axeia, error handling is what I'm trying to do. I don't need an assert because I'm not trying to prove that something can or can't happen, and I don't need a plain if/else because I want the user to know if their inputs are invalid. Quote Link to comment https://forums.phpfreaks.com/topic/157364-when-to-use-custom-exceptions/#findComment-829669 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.