如何在 java 中的 if-else 函数中引发错误
How can I raise an error in if-else function in java
我所做的所有谷歌搜索似乎都集中在 "catching" 错误上。如果满足某些条件,我希望能够提高自己的水平。我尝试使用 Error() class 及其子 classes 但 Eclipse 无法识别它们。
这就是我想要做的:
if(some_condition) {
foobar();
}
else {
// raise an error
}
我知道这是个愚蠢的问题,但我已经完成了谷歌搜索,我认为有人可以帮助我。
提前致谢!
谢谢大家!如果您以后正在阅读本文,请看这里:
Java 中的错误指的是您不应尝试捕获的问题
异常是指您可能想要捕获的错误。
这是我的 "fixed" 代码:
if(some_condition) {
foobar();
}
else {
throw new RuntimeError("Bad.");
}
我使用 RuntimeError()
是因为,正如一个答案所指出的,我不必事先声明我正在抛出一个错误,而且由于我依赖于一个条件,这非常有用。
谢谢大家!
尝试抛出异常:
public void yourMethod() throws Exception {
if (some_condition) {
foobar();
}
else {
throw new Exception("Something bad happened.");
}
}
给你:
throw new java.lang.Error("this is very bad");
更习惯于抛出异常的子类。特别是 RuntimeException 是未检查的(例如,方法不需要声明它们可能会抛出它)。 (嗯,Error 也是,但它应该是为不可恢复的东西保留的)。
throw new java.lang.RuntimeException("this is not quite as bad");
注意:您实际上不必在那个时候正确构建它们。你可以扔 pre-constructed 个。但是构造它们的一个好处是它们记录了它们所在的代码行以及构造时发生的完整 call-stack,因此当你抛出它时构造一个新的代码确实注入它非常有帮助诊断信息。
Java 中的错误称为异常 (See this)。
if(some_condition){
foobar();
} else {
throw new Exception();
}
形式上,错误是从 Throwable 扩展的任何 class。
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
您是否试图仅在控制台中引发错误?还是您要向用户显示错误?
在这种情况下,您可以使用 JOptionPane 创建错误对话框。
无需感到愚蠢:-)
您可以创建自己的例外,例如,
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;
}
}
我所做的所有谷歌搜索似乎都集中在 "catching" 错误上。如果满足某些条件,我希望能够提高自己的水平。我尝试使用 Error() class 及其子 classes 但 Eclipse 无法识别它们。
这就是我想要做的:
if(some_condition) {
foobar();
}
else {
// raise an error
}
我知道这是个愚蠢的问题,但我已经完成了谷歌搜索,我认为有人可以帮助我。
提前致谢!
谢谢大家!如果您以后正在阅读本文,请看这里:
Java 中的错误指的是您不应尝试捕获的问题
异常是指您可能想要捕获的错误。
这是我的 "fixed" 代码:
if(some_condition) {
foobar();
}
else {
throw new RuntimeError("Bad.");
}
我使用 RuntimeError()
是因为,正如一个答案所指出的,我不必事先声明我正在抛出一个错误,而且由于我依赖于一个条件,这非常有用。
谢谢大家!
尝试抛出异常:
public void yourMethod() throws Exception {
if (some_condition) {
foobar();
}
else {
throw new Exception("Something bad happened.");
}
}
给你:
throw new java.lang.Error("this is very bad");
更习惯于抛出异常的子类。特别是 RuntimeException 是未检查的(例如,方法不需要声明它们可能会抛出它)。 (嗯,Error 也是,但它应该是为不可恢复的东西保留的)。
throw new java.lang.RuntimeException("this is not quite as bad");
注意:您实际上不必在那个时候正确构建它们。你可以扔 pre-constructed 个。但是构造它们的一个好处是它们记录了它们所在的代码行以及构造时发生的完整 call-stack,因此当你抛出它时构造一个新的代码确实注入它非常有帮助诊断信息。
Java 中的错误称为异常 (See this)。
if(some_condition){
foobar();
} else {
throw new Exception();
}
形式上,错误是从 Throwable 扩展的任何 class。
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
您是否试图仅在控制台中引发错误?还是您要向用户显示错误?
在这种情况下,您可以使用 JOptionPane 创建错误对话框。
无需感到愚蠢:-) 您可以创建自己的例外,例如,
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;
}
}