如何在java中的if-else函数中引发错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34938443/
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
How can I raise an error in if-else function in java
提问by Eli Dinkelspiel
All the googling I've done seems focused on "catching" errors. I want to be able to raise my own if certain conditions are met. I tried using the Error() class and its subclasses but Eclipse doesn't recognize them.
我所做的所有谷歌搜索似乎都集中在“捕捉”错误上。如果满足某些条件,我希望能够提高自己的水平。我尝试使用 Error() 类及其子类,但 Eclipse 无法识别它们。
This is what I want to do:
这就是我想要做的:
if(some_condition) {
foobar();
}
else {
// raise an error
}
Stupid question, I know, but I've done my googling and I figure someone out there would be able to help me.
愚蠢的问题,我知道,但我已经完成了我的谷歌搜索,我想有人可以帮助我。
Thanks in advance!
提前致谢!
Thanks everyone! If you are reading this in the future, here's the skinny:
谢谢大家!如果您将来要阅读本文,请查看以下内容:
Errors in Java refer to problems that you should NOT try to catch
Exceptions refer to errors that you may want to catch.
Java 中的错误是指您不应该尝试捕捉的问题
异常是指您可能想要捕获的错误。
Here's my "fixed" code:
这是我的“固定”代码:
if(some_condition) {
foobar();
}
else {
throw new RuntimeError("Bad.");
}
I used RuntimeError()
because, as one answer pointed out, I don't have to declare that I'm throwing an error beforehand, and since I'm relying on a condition, that's very useful.
我使用RuntimeError()
是因为,正如一个答案所指出的,我不必事先声明我正在抛出错误,而且由于我依赖于一个条件,这非常有用。
Thanks all!
谢谢大家!
采纳答案by G. Sylvie Davies
Here you go:
干得好:
throw new java.lang.Error("this is very bad");
More idiomatic to throw a subclass of Exception. RuntimeException in particular is unchecked (e.g., methods don't need to declare that they might throw it). (Well, so is Error, but it's supposed to be reserved for unrecoverable things).
更惯用的方法是抛出 Exception 的子类。RuntimeException 尤其是未经检查的(例如,方法不需要声明它们可能会抛出它)。(好吧,错误也是如此,但它应该是为不可恢复的事情保留的)。
throw new java.lang.RuntimeException("this is not quite as bad");
Note: you don't actually have to construct them right at that moment. You can throw pre-constructed ones. But one thing nice about constructing them is they record the line of code they were on and the complete call-stack that happened at the time of construction, and so constructing a new one right when you throw it does inject it with very helpful diagnostic information.
注意:您实际上不必在那一刻构建它们。你可以扔预先构建的。但是构建它们的一个好处是它们记录了它们所在的代码行以及构建时发生的完整调用堆栈,因此在您抛出它时构建一个新的代码确实向它注入了非常有用的诊断信息.
回答by Tim Biegeleisen
Try throwing an exception:
尝试抛出异常:
public void yourMethod() throws Exception {
if (some_condition) {
foobar();
}
else {
throw new Exception("Something bad happened.");
}
}
回答by Manuel Vieda
An error in Java is called Exception (See this).
Java 中的错误称为异常(请参阅此)。
if(some_condition){
foobar();
} else {
throw new Exception();
}
Formally, an error is any class that extends from Throwable.
形式上,错误是从 Throwable 扩展的任何类。
The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement. Similarly, only this class or one of its subclasses can be the argument type in a catch clause. For the purposes of compile-time checking of exceptions, Throwable and any subclass of Throwable that is not also a subclass of either RuntimeException or Error are regarded as checked exceptions.
...
https://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html
Throwable 类是 Java 语言中所有错误和异常的超类。只有作为此类(或其子类之一)的实例的对象才会被 Java 虚拟机抛出或可以被 Java throw 语句抛出。同样,只有此类或其子类之一可以是 catch 子句中的参数类型。出于编译时异常检查的目的,Throwable 和 Throwable 的任何子类(不是 RuntimeException 或 Error 的子类)都被视为已检查异常。
...
https://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html
回答by Shivam Sinha
Are you trying to raise a error in console only? Or are you trying to show an error to the user?
您是否试图仅在控制台中引发错误?或者您是否试图向用户显示错误?
In which case you can use a JOptionPane to create a error dialog box.
在这种情况下,您可以使用 JOptionPane 创建错误对话框。
回答by Tamias
No need to feel dumb :-) You can create your own exception, e.g.,
无需感到愚蠢:-) 您可以创建自己的异常,例如,
public class UnknownUnitException extends Exception{
private String message = null;
public UnknownUnitException() {
super();
}
public UnknownUnitException(String message) {
super(message);
this.message = message;
}
public UnknownUnitException(Throwable cause) {
super(cause);
}
@Override
public String toString() {
return message;
}
@Override
public String getMessage() {
return message;
}
}
I'd recommend Lesson: Exceptions at the Java Tutorials