54

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.
Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
James
  • 2,306
  • 1
  • 22
  • 23
  • In Java it would be an Error rather than an Exception... That's the JVM's way of telling us that something went terribly wrong. – Andreas Dolk Jul 02 '10 at 12:43
  • @Andreas_D - What do you mean:) I can't connect your post to the question. – Petar Minchev Jul 02 '10 at 12:48
  • 1
    @Petar - In Java we have two kinds of Throwables: Errors and Exceptions. Errors are usually thrown by the virtual machine. My understanding of the IOE is that the VM detects that a method cannot be executed. I guess, in Java you would have to modify loaded bytecode at runtime to have this effect while in C# a crashed or unloaded dll could lead to this exception - and in this case, the JVM would complain - with an Error. – Andreas Dolk Jul 02 '10 at 12:54
  • 2
    @Andreas - The IOE is a perfectly valid exception to be thrown by the programmer. In fact I think it is commonly used. – Petar Minchev Jul 02 '10 at 12:56
  • @Andreas_D: Very, very good point about also looking at `Error` subclasses, although in this specific case, I think it would be classed an `Exception` instead (probably a `RuntimeException`). – T.J. Crowder Jul 02 '10 at 12:58
  • @Petar - this is what I found at msdn: *An InvalidOperationException exception is thrown in cases when the failure to invoke a method is caused by a reason other than an invalid argument. This may be thrown by .NET Framework methods when the underlying Win32 method cannot be invoked.*. Is this really thrown by a programmer? – Andreas Dolk Jul 02 '10 at 13:04
  • 1
    @Andreas - No doubt that .NET Framework can throw it, but I have seen in it used in examples including MSDN - http://msdn.microsoft.com/en-us/library/ms173163.aspx. I mean they encourage to use the exception as a first class citizen:) – Petar Minchev Jul 02 '10 at 13:14

2 Answers2

73

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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Carl
  • 7,538
  • 1
  • 40
  • 64
  • +1 That's pretty much a direct match. (The .Net exception's name isn't very good, is it?) – T.J. Crowder Jul 02 '10 at 12:53
  • @T.J.: I kind of like the .Net name, especially if it were paired with the `UnsupportedOperationException` naming. I think those two names could be used to communicate the sometimes vs always distinction between the two. The .Net equivalent of an `UnsupportedOperationException` (I point it out in my comment on your answer) is *okay* name-wise (`NotSupportedException`), but both languages made crappy pairings of the two types, IMO. – Carl Jul 02 '10 at 13:01
  • 1
    +1 An illegal state causes an invalid operation. Quite logical:) – Petar Minchev Jul 02 '10 at 13:02
  • 8
    I much prefer the C# exception's name - it's not the state that's illegal (the state's fine), it's the operation being attempted that's illegal in the current state. `IllegalStateException` implies the object has gotten into an illegal state (oh no!), which isn't the case - it's just that someone's tried to do something wrong with it, and gotten caught (phew). – Karu Jul 29 '13 at 07:01
4

Petar pointed me to this example code (from msdn)

void WriteLog()
{
    if (!this.logFile.CanWrite)
    {
        throw new System.InvalidOperationException("Logfile cannot be read-only");
    }
    // Else write data to the log and return.
}

So in this context you could use an IllegalStateException, although it says:

Thrown when an action is attempted at a time when the virtual machine is not in the correct state.

And an illegal VM state is definitly not the issue in the above reference example. Here, the problem is that the object is invalid, because it references a read-only logfile.

My own advice: just define a custom exception like

package com.pany.project;
public class InvalidOperationException extends RuntimeException {

   // add constructors with call to super as needed

}

To me, that's much easier then trying to find the best fitting Exception from the java.lang package.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • 1
    This hurts interoperability though. If you can find a standard exception that fits, creating new ones willy nilly leads to every library defining their own. It's nice having the reusable knowledge of the `java.lang` exception types, what they mean, etc. – BAF Jul 08 '15 at 18:37