Java equivalent of .NET System.InvalidOperationException

I am not as familiar with Java exception packages as I am with .NET. I am in a situation where, when programming in C #, I would throw a System.InvalidOperationException .

Before creating my own subclass of java.lang.RuntimeException I need to know if there is a similar type of exception that I should use in Java.

Exact scenario:

My class is a value object that provides an int intValue() method that returns an int. However, in some situations, the current value cannot be represented as int, so this class also provides boolean isInteger() so that API users can know when intValue() can be safely called.

If the caller calls intValue() when isInteger() is false , an exception should be thrown.

And the question is: what type of exception?

I know this question may not have the right answer, but given that I do not have much experience developing the Java API, I want to know from other Java developers what they would expect in this scenario.

thank

+62
java design api exception-handling
Aug 01 2018-10-10T00:
source share
1 answer

Throw IllegalStateException :

Signals that the method was called at an illegal or inappropriate time. In other words, the Java environment or Java application is not in the appropriate state for the requested operation.

+71
Aug 01 2018-10-10T00:
source share



All Articles