在流程之前验证设置数据时,我应该使用哪个 SPL 异常?
我正在努力弄清楚何时使用几个 PHP SPL 异常,特别是在下面的场景中,
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
}
}
}
我构建了一个类。然后,用户在该对象上设置一些数据,并调用一个进程。该过程要做的第一件事是验证对象上的数据,以确保所需数据存在、数据组合正确等,如果不正确,则需要抛出异常。
那么我会抛出什么异常?
我的验证实际上检查两种情况,
- 丢失数据,又称尚未设置的数据。
- 数据组合不良。
对于#1,我在 BadMethodCallException
、RuntimeException
和 LogicException
之间左右为难。对于#2,我认为这只是一个LogicException
?
那么,我使用哪些呢?
注意:在有人问之前,我不能将所需的数据作为构造函数中的参数,因为某些数据仅在设置其他数据时才需要,等等。
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,
- Missing data, a.k.a. data that has not been set.
- Bad combination of data.
For #1, I'm torn between BadMethodCallException
, 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您必须使用SPL异常,那就是
RuntimeException
。这是指只能在运行时检测到的错误(例如错误的输入数据)。LogicException
将是一个不合适的选择,因为它指的是逻辑错误出现在您的程序中,而不是出现在它接收到的数据中。当您的程序检测到必须始终为真的条件不满足时,将LogicException
视为紧急按钮(将此与应该的条件进行对比) > 对于程序执行其预期功能来说是正确的)。BadMethodCallException
也不合适,因为它代表一个您的逻辑所需的某些数据可能会丢失,但是如果没有正确数量的参数,就无法调用方法。
站在你的立场上,我要么定义自己的异常(从
RuntimeException
派生),要么直接使用RuntimeException
。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 aLogicException
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 anSome 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 useRuntimeException
directly.考虑到预先存在的异常似乎都无法满足您的需求,为什么不创建自己的异常?
例如,您可以拥有:
ValidationException
的超类,这将扩展Exception
。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**][1].*
Considering that none of the pre-existing exceptions seems to answer your needs, why not create your own exceptions ?
For instance, you could have :
ValidationException
, that would extendException
.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**][1].*
我个人会在每个
中抛出
方法。InvalidArgumentException
>set*()I would personally throw an
InvalidArgumentException
in eachset*()
method.