Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.
like image 910
James Avatar asked Jul 02 '10 12:07

James


People also ask

Is Java similar to C?

C is a middle-level language as it binds the bridges between machine-level and high-level languages. Java is a high-level language as the translation of Java code takes place into machine language, using a compiler or interpreter. C is only compiled and not interpreted. Java is both compiled and interpreted.

Is Java similar to C or C++?

These two languages are very similar in terms of syntax and language features. They are so similar that if you're shown some portion of C++ code from a project and asked whether it's C++ or Java code, you may confuse yourself.

Is C# equivalent to Java?

Java and C# are incredibly similar. Both languages are somewhat derived from C++ and from similar first principles. Java was developed in 1995 to create a language with a simpler programming model than C++ while still preserving some of the same syntax of the language to facilitate developers transitioning to it.

How different is C and C in Java?

C is a compiled language that is it converts the code into machine language so that it could be understood by the machine or system. Java is an Interpreted language that is in Java, the code is first transformed into bytecode and that bytecode is then executed by the JVM (Java Virtual Machine).


2 Answers

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.

like image 69
Carl Avatar answered Sep 22 '22 05:09

Carl


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.

like image 39
Andreas Dolk Avatar answered Sep 25 '22 05:09

Andreas Dolk