Ivan Ivan - 1 year ago 46
Java Question

What exception to throw when a property or a method being accessed is temporarily unavailable?

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

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.

I am interested in answers for Java and .Net platforms.

Answer Source

java.lang.IllegalStateException

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download