在java中引发异常后保持程序运行
我已经写了一段代码,我想在抛出异常后继续运行。 这是我的密码:在java中引发异常后保持程序运行,java,Java,我已经写了一段代码,我想在抛出异常后继续运行。 这是我的密码: public class SquareEquationException extends Exception{ public SquareEquationException(){ super("roots are not real numbers"); } public static void SquareEquation() throws SquareEquationExcept
public class SquareEquationException extends Exception{
public SquareEquationException(){
super("roots are not real numbers");
}
public static void SquareEquation() throws SquareEquationException{
double a,b,c,sqr;
int flag;
Scanner in = new Scanner(System.in);
System.out.println("enter ax^2:");
a=in.nextDouble();
System.out.println("enter bx:");
b=in.nextDouble();
System.out.println("enter c:");
c=in.nextDouble();
sqr=((b*b)-(4*a*c));
if(sqr<0)
throw new SquareEquationException();
else{
sqr=Math.sqrt(sqr);
double x1=(-b+sqr)/(2*a);
double x2 = (-b-sqr)/(2*a);
if(x1==x2)
System.out.println("root is:" + x1);
else
System.out.println("x1 is:"+x1 +"\n"+ "x2 is:"+x2 );
}
System.out.println("enter 1 to continue or any key to exit");
flag = in.nextInt();
while(flag==1){
System.out.println("enter ax^2:");
a=in.nextDouble();
System.out.println("enter bx:");
b=in.nextDouble();
System.out.println("enter c:");
c=in.nextDouble();
sqr=((b*b)-(4*a*c));
if(sqr<0){
throw new SquareEquationException();
}
else{
sqr=Math.sqrt(sqr);
double x1=(-b+sqr)/(2*a);
double x2 = (-b-sqr)/(2*a);
if(x1==x2)
System.out.println("root is:" + x1);
else
System.out.println("x1 is:"+x1 +"\n"+ "x2 is:"+x2 );
}
System.out.println("enter 1 to continue or any key to exit");
flag=in.nextInt();
}
}
public static void main(String[] args) throws SquareEquationException{
SquareEquation();
}
}
公共类SquareEquationException扩展异常{
public SquareEquationException(){
超级(“根不是实数”);
}
公共静态void SquareEquation()引发SquareEquationException{
双a、b、c、sqr;
int标志;
扫描仪输入=新扫描仪(系统输入);
System.out.println(“输入ax^2:”);
a=in.nextDouble();
System.out.println(“输入bx:”);
b=in.nextDouble();
System.out.println(“输入c:”);
c=in.nextDouble();
sqr=((b*b)-(4*a*c));
如果(sqr您也必须捕获异常。请将其包装在try-catch中。此外,您还需要使用循环再次运行代码。例如
while(true){
try{
// Code you want to execute that can throw an error£
SquareEquation();
// Exit loop if execution ended without an exception
break;
}
catch(SquareEquationException ex){
// Exception logic (such as showing a message) or just printing the trace (but this doesn't help a user)
ex.printStackTrace();
}
}
您可以将其包装在一个try-catch中。此外,您还可以避免重复代码,并应在完成后关闭扫描仪。例如
import java.util.Scanner;
public class SquareEquationException extends Exception {
public SquareEquationException() {
super("roots are not real numbers");
}
public static void SquareEquation() throws SquareEquationException {
double a, b, c, sqr;
int flag = 1;
Scanner in = new Scanner(System.in);
while (flag == 1) {
try{
System.out.println("enter ax^2:");
a = in.nextDouble();
System.out.println("enter bx:");
b = in.nextDouble();
System.out.println("enter c:");
c = in.nextDouble();
sqr = ((b * b) - (4 * a * c));
if (sqr < 0)
throw new SquareEquationException();
else {
sqr = Math.sqrt(sqr);
double x1 = (-b + sqr) / (2 * a);
double x2 = (-b - sqr) / (2 * a);
if (x1 == x2)
System.out.println("root is:" + x1);
else
System.out.println("x1 is:" + x1 + "\n" + "x2 is:" + x2);
}
} catch(SquareEquationException e){
System.out.println(e.getMessage());
}
System.out.println("enter 1 to continue or any key to exit");
flag = in.nextInt();
}
in.close();
}
public static void main(String[] args) throws SquareEquationException {
SquareEquation();
}
}
import java.util.Scanner;
公共类SquareEquationException扩展了异常{
public SquareEquationException(){
超级(“根不是实数”);
}
公共静态void SquareEquation()引发SquareEquationException{
双a、b、c、sqr;
int标志=1;
扫描仪输入=新扫描仪(系统输入);
while(标志==1){
试一试{
System.out.println(“输入ax^2:”);
a=in.nextDouble();
System.out.println(“输入bx:”);
b=in.nextDouble();
System.out.println(“输入c:”);
c=in.nextDouble();
sqr=((b*b)-(4*a*c));
如果(sqr<0)
抛出新的SquareEquationException();
否则{
sqr=数学sqrt(sqr);
双x1=(-b+sqr)/(2*a);
双x2=(-b-sqr)/(2*a);
如果(x1==x2)
System.out.println(“根为:“+x1”);
其他的
System.out.println(“x1是:“+x1+”\n“+”x2是:“+x2”);
}
}捕获(平方方程异常e){
System.out.println(e.getMessage());
}
System.out.println(“输入1继续或任何键退出”);
flag=in.nextInt();
}
in.close();
}
公共静态void main(字符串[]args)引发SquareEquationException{
平方方程();
}
}
使用try-catch语句和异常处理您的Exeption
通过main
方法填充,因此您的程序中止。您需要catch
异常并相应地采取行动。如果发生异常
,则立即停止执行,并执行第一个匹配的catch
块输入。有关更多信息,您可能希望查看。但是在catch块中添加一些异常处理,忽略异常从来都不是一件好事。仅此一点可能还不够。我认为OP应该将整个代码包装在一个循环中,以便在出现异常时重新执行它。@Turing85确实如此。这将停止程序ram不会因为错误而停止,但在执行一次后,他的程序仍会停止。