在流程之前验证设置数据时,我应该使用哪个 SPL 异常?

发布于 2024-10-21 07:05:52 字数 1201 浏览 2 评论 0原文

我正在努力弄清楚何时使用几个 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. 丢失数据,又称尚未设置的数据。
  2. 数据组合不良。

对于#1,我在 BadMethodCallExceptionRuntimeExceptionLogicException 之间左右为难。对于#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,

  1. Missing data, a.k.a. data that has not been set.
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

救星 2024-10-28 07:05:52

如果您必须使用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 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.

笙痞 2024-10-28 07:05:52

考虑到预先存在的异常似乎都无法满足您的需求,为什么不创建自己的异常?

例如,您可以拥有:

  • 一个名为 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 :

  • 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**][1].*

黯然#的苍凉 2024-10-28 07:05:52

我个人会在每个 中抛出 InvalidArgumentException >set*() 方法。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文