Is there a Java equivalent of C#'s InvalidOperationException?

James picture James · Jul 2, 2010 · Viewed 20.8k times · Source

I'm converting some C# code to Java and I need to include an exception that is similar to C#'s InvalidOperationException. Does such a thing exist? Also is there a list of equivalent exception types in the two languages? Thanks.


I think in my particular case IllegalStateException is most appropriate. Thanks for all the responses.

Answer

Carl picture Carl · Jul 2, 2010

Probably IllegalStateException.

From what I read about InvalidOperationException: "The exception that is thrown when a method call is invalid for the object's current state."

For 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."

Depending on how you are using InvalidOperationException, I could also see IllegalArgumentException and UnsupportedOperationException being what you want. The former implies that, in general, the method is fine to call, it was just passed garbage this time; the latter implies that the method is never appropriate to call for this instance (unlike IllegalStateException, which implies that it might be appropriate to call the subject method sometimes, just not at the moment).


I am not aware of a general c# <=> Java translation of exceptions.