是否有与 C# 的 InvalidOperationException 等效的 Java?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3165701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a Java equivalent of C#'s InvalidOperationException?
提问by James
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.
我正在将一些 C# 代码转换为 Java,我需要包含一个类似于 C# 的 InvalidOperationException 的异常。这样的事情存在吗?还有两种语言的等效异常类型列表吗?谢谢。
我认为在我的特殊情况下 IllegalStateException 是最合适的。感谢所有的回应。
回答by Carl
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."
从我读到的关于 InvalidOperationException 的内容来看:“当方法调用对于对象的当前状态无效时抛出的异常。”
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."
对于IllegalStateException:“在非法或不适当的时间调用方法的信号。换句话说,Java 环境或 Java 应用程序未处于所请求操作的适当状态。”
Depending on how you are using InvalidOperationException, I could also see IllegalArgumentExceptionand UnsupportedOperationExceptionbeing 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 neverappropriate 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).
根据您的使用方式InvalidOperationException,我也可以看到IllegalArgumentException并UnsupportedOperationException成为您想要的东西。前者暗示,一般情况下,该方法可以调用,这次只是传递垃圾;后者意味着该方法永远不适合调用此实例(与 不同IllegalStateException,这意味着有时调用主题方法可能是合适的,只是目前不合适)。
I am not aware of a general c# <=> Java translation of exceptions.
我不知道一般的 c# <=> Java 异常翻译。
回答by Andreas Dolk
Petar pointed me to this example code (from msdn)
Petar 向我指出了这个示例代码(来自 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:
因此,在这种情况下,您可以使用 IllegalStateException,尽管它说:
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.
并且非法 VM 状态绝对不是上述参考示例中的问题。这里的问题是该对象无效,因为它引用了一个只读日志文件。
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.langpackage.
对我来说,这比尝试从java.lang包中找到最合适的异常要容易得多。

