开发者

Which SPL Exception should I use when validating set data before a process?

I'm struggling to get my head around when to use a couple of the PHP SPL Exceptions, specifically in the below scenario,

class MyClass {
    protected $data1;
    protected $data2;

    public function setData1($data1) {
        $this->data1 = $data1;
    }

    public function setData2($data2) {
        $this->data2 = $data2;
    }

    public function invokeProcess() {
        $this->validateData();
    }

    protected function validateData() {
        if(!$this->data1) {
            // Which Exception do I throw? See explanation below
        }

        if($this->data1 && $this->data2) {
            // Which Exception do I throw? See explanation below
        }
    }
}

I have a class which is constructed. The user then sets some data on the object, and invokes a process. The first thing this process does is validate the data on the object to make sure required data is present, data combinations are correct, etc, and if they aren't, an Exception needs to be thrown.

So what Exceptions do I throw?

My validation checks for two scenarios really,

  1. Missing data, a.k.a. data that has not been set.
  2. Bad combination of data.

For #1, I'm torn between Ba开发者_StackOverflow中文版dMethodCallException, RuntimeException, and LogicException. And for #2, I think it's just a LogicException?

So, which ones so I use?

Note: Before anyone asks, I can't have required data as parameters in the constructor due to some data only being required when other data is set, etc.


If you have to use an SPL exception, that would be RuntimeException. That's the one that refers to an error that can only be detected at runtime (such as bad input data).

LogicException would be an inappropriate choice, as it refers to a logic error in your program, not in the data it receives. Think of a LogicException as a panic button when your program detects that a condition that must always be true is not (contrast this with a condition that should be true for the program to perform its intended function).

BadMethodCallException would also be inappropriate since it represents an

Exception thrown if a callback refers to an undefined method or if some arguments are missing.

Some data your logic needs may be missing, but there's no method call without the correct number of arguments there.

In your shoes I would either define my own exceptions (derived from RuntimeException), or use RuntimeException directly.


Considering that none of the pre-existing exceptions seems to answer your needs, why not create your own exceptions ?

For instance, you could have :

  • A super-class called ValidationException, that would extend Exception.
  • That would be inherited by two sub-classes :
    • ValidationException_MissingData
    • ValidatonException_BadCombination


You do not necessarily have to use the pre-existing SPL exceptions, if they don't fit your situation : the exceptions mecanism is powerful enough to let you define whatever you need -- and that's what's done by most Frameworks, for instance.


For more details on the how, see Extending Exceptions.


I would personally throw an InvalidArgumentException in each set*() method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜