Jump to content

When to use custom exceptions


AmandaF

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.