Hi I'm trying to create a custom exception handler for a quadratic formula solver. The exception handling class that I have is meant to throw the answer anytime b*b - 4*a*c is negative, and it looks like this...
嗨,我正在尝试为二次公式求解器创建自定义异常处理程序。我拥有的异常处理类意味着随时抛出答案b * b - 4 * a * c为负数,看起来像这样......
public class NegativeDiscriminantException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
public NegativeDiscriminantException(String a){
System.out.println(a);
Quadratic.solver();
}
}
The "Quadratic.solver();" is intended to restart the variable inquiry process from the beginning. But here is my method to solve it from the Quadratic class...
“Quadratic.solver();”旨在从头开始重新启动变量查询过程。但这是我从Quadratic类解决它的方法......
static public double root( double A, double B, double C )
throws IllegalArgumentException, ArithmeticException, NegativeDiscriminantException {
try{
if (A == 0.0) {
throw new ArithmeticException();
}
}
catch (ArithmeticException e){
System.out.println("'A' can't be zero.");
solver();
}
double disc = B*B - 4*A*C;
try{
if (disc < 0){
throw new NegativeDiscriminantException("The discriminant is negative.");
}
}
catch (NegativeDiscriminantException e){
System.out.println("The discriminant is negative.");
}
return (-B + Math.sqrt(disc)) / (2*A);
}
And I get a compiler error...
我收到编译错误......
Exception in thread "main" java.lang.Error: Unresolved compilation problem: Unhandled exception type NegativeDiscriminantException at Quadratic.solver(Quadratic.java:62) at Quadratic.main(Quadratic.java:34)
线程“main”中的异常java.lang.Error:未解决的编译问题:Quadratic.main中的Quadratic.solver(Quadratic.java:62)处的未处理异常类型NegativeDiscriminantException(Quadratic.java:34)
I'm new with using custom exception handlers. Could you please help me with what I'm doing wrong?
我是使用自定义异常处理程序的新手。你能帮我解决我做错的事吗?
3 个解决方案
#1
This isn't how you use exceptions. The idea is if there's an exceptional situation, you throw the exception and the caller of your class has to deal with it (or re-throw it). You don't catch an exception you just threw. So get rid of your catch
clauses (and the surrounding try
).
这不是您使用异常的方式。这个想法是,如果有一个异常的情况,你抛出异常,你的类的调用者必须处理它(或重新抛出它)。你没有抓住你刚刚抛出的异常。所以摆脱你的catch子句(以及周围的尝试)。
Your exception class is also bad. It shouldn't be responsible for anything other than describing the exception. Let the caller decide what to do. So get rid of the call to Quadratic.solver();
.
你的异常类也很糟糕。除了描述异常之外,它不应该负责任何其他事情。让来电者决定做什么。所以摆脱对Quadratic.solver();的调用。
The compilation error is because whatever code is calling root
needs to either catch your exceptions, or add a throws
declaration to its signature.
编译错误是因为调用root的任何代码都需要捕获异常,或者在其签名中添加throws声明。
#2
NegativeDiscriminantException
is a checked Exception
, so you must place any calls to a method that may throw this exception within a try/catch (or declare the caller method to throw it so it can be caught upstream).
NegativeDiscriminantException是一个已检查的Exception,因此您必须对可能在try / catch中抛出此异常的方法进行任何调用(或声明调用方法以将其抛出以便可以在上游捕获)。
try{
root(...);
}catch(NegativeDiscriminantException e){
e.printStackTrace();//
Quadratic.solver();//placed here rather than within the Exception constructor
}
If you do not want to place calls within a try/catch (or declare the caller to throw the Exception), use an unchecked Exception
(eg have NegativeDiscriminantException
extend RuntimeException
, although I do recommend reading about the 'controversy', and I do recommend not requiring the Exception to perform any other tasks other than defining the problem that occurred)
如果你不想在try / catch中调用(或声明调用者抛出异常),请使用未经检查的异常(例如,使用NegativeDiscriminantException扩展RuntimeException,尽管我建议阅读'争议',我建议除了定义发生的问题之外,不要求Exception执行任何其他任务
#3
The root cause of your error is as @copeg mentioned that you do not handle the exception which could be thrown by the call to Quadratic.solver()
in the constructor of your Exception.
你的错误的根本原因是@copeg提到你没有处理在你的Exception的构造函数中调用Quadratic.solver()时可能抛出的异常。
However your usage of Exceptions is rather peculiar. Exceptions were invented to be able to handle errors at the right level and be able to bail out of nested code without using goto
statements.
但是,您对异常的使用是相当特殊的。发明异常是为了能够在正确的级别处理错误,并且能够在不使用goto语句的情况下摆脱嵌套代码。
You are also calling the solver()
method in the exception handling code which looks like it is recursing. Sooner or later this will cause your program to run out of stack space.
您还在异常处理代码中调用solver()方法,该代码看起来像是在递归。迟早会导致程序耗尽堆栈空间。
It would be better to handle the exceptions in the solver method so you can loop there.
最好在求解器方法中处理异常,这样就可以在那里循环。
public double solver() {
boolean isAnswered = false;
double answer;
while (!isAnswered) {
double a = askCoefficient("A:");
double b = askCoefficient("B:");
double c = askCoefficient("C:");
try {
answer = root(a,b,c);
isAnswered = true;
} catch (Exception e) {
System.out.println("ERROR: " + e);
}
}
return answer
}
public double root( double A, double B, double C ) throws ArithmeticException, NegativeDiscriminantException {
if (A == 0.0) {
throw new ArithmeticException();
}
double disc = B*B - 4*A*C;
if (disc < 0){
throw new NegativeDiscriminantException("The discriminant is negative.");
}
return (-B + Math.sqrt(disc)) / (2*A);
}
Now the exceptions are handled where the logic can deal with them.
现在处理异常处理逻辑可以处理它们。
#1
This isn't how you use exceptions. The idea is if there's an exceptional situation, you throw the exception and the caller of your class has to deal with it (or re-throw it). You don't catch an exception you just threw. So get rid of your catch
clauses (and the surrounding try
).
这不是您使用异常的方式。这个想法是,如果有一个异常的情况,你抛出异常,你的类的调用者必须处理它(或重新抛出它)。你没有抓住你刚刚抛出的异常。所以摆脱你的catch子句(以及周围的尝试)。
Your exception class is also bad. It shouldn't be responsible for anything other than describing the exception. Let the caller decide what to do. So get rid of the call to Quadratic.solver();
.
你的异常类也很糟糕。除了描述异常之外,它不应该负责任何其他事情。让来电者决定做什么。所以摆脱对Quadratic.solver();的调用。
The compilation error is because whatever code is calling root
needs to either catch your exceptions, or add a throws
declaration to its signature.
编译错误是因为调用root的任何代码都需要捕获异常,或者在其签名中添加throws声明。
#2
NegativeDiscriminantException
is a checked Exception
, so you must place any calls to a method that may throw this exception within a try/catch (or declare the caller method to throw it so it can be caught upstream).
NegativeDiscriminantException是一个已检查的Exception,因此您必须对可能在try / catch中抛出此异常的方法进行任何调用(或声明调用方法以将其抛出以便可以在上游捕获)。
try{
root(...);
}catch(NegativeDiscriminantException e){
e.printStackTrace();//
Quadratic.solver();//placed here rather than within the Exception constructor
}
If you do not want to place calls within a try/catch (or declare the caller to throw the Exception), use an unchecked Exception
(eg have NegativeDiscriminantException
extend RuntimeException
, although I do recommend reading about the 'controversy', and I do recommend not requiring the Exception to perform any other tasks other than defining the problem that occurred)
如果你不想在try / catch中调用(或声明调用者抛出异常),请使用未经检查的异常(例如,使用NegativeDiscriminantException扩展RuntimeException,尽管我建议阅读'争议',我建议除了定义发生的问题之外,不要求Exception执行任何其他任务
#3
The root cause of your error is as @copeg mentioned that you do not handle the exception which could be thrown by the call to Quadratic.solver()
in the constructor of your Exception.
你的错误的根本原因是@copeg提到你没有处理在你的Exception的构造函数中调用Quadratic.solver()时可能抛出的异常。
However your usage of Exceptions is rather peculiar. Exceptions were invented to be able to handle errors at the right level and be able to bail out of nested code without using goto
statements.
但是,您对异常的使用是相当特殊的。发明异常是为了能够在正确的级别处理错误,并且能够在不使用goto语句的情况下摆脱嵌套代码。
You are also calling the solver()
method in the exception handling code which looks like it is recursing. Sooner or later this will cause your program to run out of stack space.
您还在异常处理代码中调用solver()方法,该代码看起来像是在递归。迟早会导致程序耗尽堆栈空间。
It would be better to handle the exceptions in the solver method so you can loop there.
最好在求解器方法中处理异常,这样就可以在那里循环。
public double solver() {
boolean isAnswered = false;
double answer;
while (!isAnswered) {
double a = askCoefficient("A:");
double b = askCoefficient("B:");
double c = askCoefficient("C:");
try {
answer = root(a,b,c);
isAnswered = true;
} catch (Exception e) {
System.out.println("ERROR: " + e);
}
}
return answer
}
public double root( double A, double B, double C ) throws ArithmeticException, NegativeDiscriminantException {
if (A == 0.0) {
throw new ArithmeticException();
}
double disc = B*B - 4*A*C;
if (disc < 0){
throw new NegativeDiscriminantException("The discriminant is negative.");
}
return (-B + Math.sqrt(disc)) / (2*A);
}
Now the exceptions are handled where the logic can deal with them.
现在处理异常处理逻辑可以处理它们。