当被访问的属性或方法暂时不可用时抛出什么异常(因为它在对象的当前状态下没有意义)?
What exception to throw when a property or a method being accessed is temporarily unavailable (as it makes no sense in the current state of the object)?
以下是一个半说明性的例子出现在我的脑海中,不要太认真,如果你得到它,试着考虑问题本身。
Following is a semi-illustrative example coming into my mind, don't take it too serious, try to consider the question itself if you get it.
A 分数具有分子和分母读写属性和 quotient 只读属性。如果分母不等于0,则商将返回分子 / 分母否则抛出异常,说明属性值未定义/可用于对象的当前状态(请不要提供 ArithmeticException 或 DivideByZeroException ,问题不在于算术,它更接近有限状态机主题。)
A fraction class having numerator and denominator read-and-write properties and quotient read-only property. If denominator does not equal 0 then quotient is to return numerator/denominator else it is to throw an exception saying that the property value is not defined/available for the current state of the object (please don't offer ArithmeticException or DivideByZeroException, the question is not about arithmetics, it is closer to the finite state machines subject).
另一个例子可以是类的对象在并行线程中执行一些复杂的计算并将结果作为字段提供 - 如果计算尚未完成,它可以抛出异常。
Another example can be an object of a class doing some complex calculations in a parallel thread and providing the result as a field - it can throw the exception if the calculations are not complete yet.
我对Java和.Net平台的答案感兴趣。
I am interested in answers for Java and .Net platforms.
推荐答案java.lang.IllegalStateException
java.lang.IllegalStateException
Signa ls是在非法或不适当的时间调用了一个方法。换句话说,Java环境或Java应用程序未处于所请求操作的适当状态。
Signals that a method has been invoked at an illegal or inappropriate time. In other words, the Java environment or Java application is not in an appropriate state for the requested operation.
更多推荐
当访问的属性或方法暂时不可用时抛出什么异常?
发布评论