先决条件和例外情况

发布于 2024-07-07 11:08:45 字数 98 浏览 6 评论 0原文

假设您有一个带有一些前置条件和后置条件的方法。 是否可以为每个未完成的前提条件创建一个异常类? 例如: 未完成 pre1 意味着抛出 notPre1Exception 实例。

Suppose you have a method with some pre and post-conditions.
Is it ok to create an exception class for each pre-condition that is not accomplished?
For example:
Not accomplishing pre1 means throwing a notPre1Exception instance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(12

咆哮 2024-07-14 11:08:46

我认为只要您计划使用和处理它们,就可以为所有异常创建不同的异常。

我发现错误/异常处理越好,后期调试软件就越容易。

例如:如果您有一个通用的例外来处理所有错误的输入,那么如果出现错误,您必须查看传递到该方法的所有内容。 如果您对所有类型的恶劣条件都有预期,您就会确切地知道该去哪里寻找。

I think it's ok to create a different exception for all your exceptions as long as you plan on using and handling them.

I have found that the better the error/exception handling the easier it is to debug software in the later stages.

For example: If you have a generic excpeiton to handle all bad input then you must look at everything that was passed into the method if there is an error. If you have an excpetion of all the types of bad conditions you will know exaclty where to look.

沉睡月亮 2024-07-14 11:08:46

在我看来这是可能的,但如果你想继续这种处理先决条件的方式,你最终会得到每个类方法有 N 个异常类。 看起来“非功能性”类呈爆炸式增长。

我一直喜欢“核心”功能不处理违反先决条件的代码(除了断言它们之外 - 这是一个很大的帮助!)。 然后可以将此代码包装在“前提条件检查器”中,该检查器确实抛出异常或以其他方式通知不满足。

It looks possible to me, but if you want to continue this way of dealing with preconditions, you'll end up with N exception classes per class method. Looks like an explosive growth of 'non functional' classes.

I always liked code where the 'core' functionality didn't handle violation of preconditions (apart from asserting them - a great help!). This code can then be wrapped in a 'precondition-checker', which does throw exceptions or notifies non-satisfaction otherwise.

可可 2024-07-14 11:08:46

作为确定是否应该创建类(例外就是类)的一种非常非常通用的方法,您应该确定是否有一些代码使该类与所有其他类(在本例中为例外)不同。

如果没有,我只需在异常中设置该字符串,然后就到此为止了。 如果您在异常中执行代码(也许是一个通用的恢复机制,可以通过调用Exception.resolve()或其他东西来处理多种情况),那么它可能会很有用。

我意识到异常并不总是遵循这个规则,但我认为这或多或少是因为语言提供的异常中不能有任何业务逻辑(因为它们不了解业务——库总是倾向于充满了 OO 规则的例外)

As a very very general way to determine if you should ever create a class or not (and an exception is a class), you should determine if there is some code that makes this class unique from all other classes (in this case exceptions).

If not, I'd just set the string in the exception and call it a day. If you are executing code in your exception (perhaps a generic recovery mechanism that can handle multiple situations by calling exception.resolve() or something), then it might be useful.

I realize that exceptions don't always follow this rule, but I think that's more or less because exceptions provided by the language can't have any business logic in them (because they don't know the business--libraries always tend to be full of exceptions to the OO rules)

っ左 2024-07-14 11:08:46

我认为本正中目标。 如果您不打算捕获不同的异常,那么抛出它们有什么意义呢? 如果你真的想抛出一个不同的,我至少会有一个共同的“PreconditionFailedException”基类,它们都是派生自的,并尝试将它们组织成某种层次结构,以便你可以捕获它们的组。 就我个人而言,我不会有不同的异常,只是有一个常见的异常,其中包含每个异常的失败详细信息。

I think Ben is on target here. What's the point in throwing different exceptions if you're not going to catch them? If you really want to throw a different one I would at least have a common "PreconditionFailedException" base class that they all derive from, and try to organize them into some sort of heirachy so you can catch groups of them. Personlly, I'd not have different ones and just have a common exception you throw with the details of the failure in each one.

流年里的时光 2024-07-14 11:08:46

不,您不应该为每个先决条件创建特定的例外,因为这将违反合同设计的原则。

实现先决条件的必然结果是它们应该成为文档的一部分,并且您应该为调用者提供必要的方法来检查所有先决条件是否有效。 (即,如果方法执行依赖于对象的状态,则检查状态的方法应该可供调用者使用)。

因此,调用者应该能够在调用您的方法之前检查是否满足所有先决条件。

为每个违反的前提条件实现特定的异常将/可能鼓励调用者在方法调用周围使用 try/catch 模式,这与契约设计的理念相矛盾。

No, you should not create a specific exception for each precondition because it will go against the principles of the Design-by-Contract.

The corollaries of implementing pre-conditions is that they should be part of the documentation and that you should provide the necessary methods for the caller to check that all the preconditions are valid. (i.e. if a method execution is relying on the status of an object, the method to check the status should be available to the caller).

So, the caller should be able to check if all prerequisites are met before calling your method.

Implementing specific exceptions for each violated pre-condition would/could encourage the caller to use the try/catch pattern around the method call, what is in contradiction with the philosophy of the Design-by-Contract.

愛上了 2024-07-14 11:08:46

我想说,只要你让它们成为未经检查的异常(Java 中 RuntimeException 的子类)就可以了。 然而,在 Java 中,最好只使用断言。

I would say it's okay as long as you make them unchecked exceptions (subclass of RuntimeException in Java). However, in Java it is better to just use assertions.

毁我热情 2024-07-14 11:08:46

如果您这样做,请确保它们都继承自另一个常见的自定义异常。

If you do this, make sure they all inherit from another common custom exception.

凝望流年 2024-07-14 11:08:45

为什么你不想定义 PreconditionFailedException(string precondition)? 为每个失败的前提条件抛出不同的异常类型是多余的。

Why wouldn't you want to define PreconditionFailedException(string precondition)? Throwing a different exception type for each failed precondition is overkill.

酷炫老祖宗 2024-07-14 11:08:45

是的,也不是。

是的 - 违反前提条件当然是抛出异常的适当时机。 抛出更具体的异常将使捕获该特定异常变得更简单。

否 - 为程序/api 中的每个先决条件声明一个新的异常类似乎有点矫枉过正。 这最终可能会导致数百或数千个异常。 这似乎在精神上和计算上都是一种浪费。

我建议对违反先决条件的情况抛出异常。 但是,我不建议为每个先决条件定义一个新的例外。 相反,我建议创建更广泛的异常类别,涵盖特定类型的前提条件违规,而不是特定的前提条件违规。 (我还建议在适合的情况下使用现有的异常。)

Yes and no.

Yes - Violating a precondition is certainly an appropriate time to throw an exception. Throwing a more specific exception will make catching that specific exception simpler.

No - Declaring a new exception class for every precondition in your program/api seems way overkill. This could result in hundreds or thousands of exceptions eventually. This seems a waste both mentally and computationally.

I would recommend throwing exceptions for precondition violations. I would not, however, recommend defining a new exception for each precondition. Instead, I would recommend creating broader classes of exceptions that cover a specific type of precondition violation, rather than a specific precondition violation. (I would also recommend using existing exceptions where they fit well.)

猫腻 2024-07-14 11:08:45

失败的前提条件应该抛出 AssertException 或类似的异常。 在调用一个方法之前,它的前提条件必须成立。 如果调用者不进行此检查,则说明程序中存在错误,或者方法 (API) 的使用不正确。

A failed precondition should throw an AssertException, or something similar. Before invoking a method, it's precondition must hold. If the caller doesn't do this check, it's a bug in the program, or an incorrect use of the method (API).

你的背包 2024-07-14 11:08:45

只有不满足先决条件才会出现罕见的例外情况。

Only if not accomplishing the pre-conditions would be a rare exceptional occurrence.

街角迷惘 2024-07-14 11:08:45

对我来说,听起来像是异常的有用用途。 它当然允许比一般的“前提条件失败”更细粒度的日志记录和调试,尽管您也可以有一个“前提条件失败”异常并将失败的前提条件放入异常消息中。

Sounds like a useful use of exceptions to me. It certainly allows more fine-grained logging and debugging than just a general 'precondition failed' although you could also have a single 'preconditions failed' exception and put what preconditions have failed into the exception message.

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