当访问的属性或方法暂时不可用时抛出什么异常?

编程入门 行业动态 更新时间:2024-10-28 02:29:12
本文介绍了当访问的属性或方法暂时不可用时抛出什么异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当被访问的属性或方法暂时不可用时抛出什么异常(因为它在对象的当前状态下没有意义)?

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.

更多推荐

当访问的属性或方法暂时不可用时抛出什么异常?

本文发布于:2023-11-12 19:29:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582327.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:抛出   属性   异常   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!