2015-11-16 54 views
0

我写了一段代码,我想在抛出异常后继续运行。 这里是我的代码:在java中抛出异常后继续运行程序

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(); 
    } 
} 

我怎能菜单/程序运行时抛出异常后? 菜单工作:如果用户程序插入1继续运行,否则程序退出。

编辑: i want to get the message in red and keep it running, so the menu will show right after/before and offer to continue

+0

使用try catch语句和异常处理 – Stultuske

+0

你'Exeption'填充通过'main'方法,因此你的程序中止。你需要“抓住”这个例外并采取相应的行动。如果发生“异常”,立即停止执行,并输入第一个匹配的“catch”块。有关更多信息,您可能需要查看[关于异常的Oracle踪迹](https://docs.oracle.com/javase/tutorial/essential/exceptions/)。 – Turing85

回答

0

你必须捕获异常也是如此。把它裹在试试看。此外,您还需要使用循环再次运行代码。如...

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(); 

    } 
} 
+0

,但在catch块中添加一些异常处理,忽略异常从来就不是好事。 – Stultuske

+0

仅此一项可能是不够的。我认为OP应该将整个代码封装在一个循环中,以便在“Exception”情况下重新执行它。 – Turing85

+0

这是@ Turing85。这只会阻止程序由于错误而停止 - 但是他的程序在一次执行后仍然停止。 –

0

您可以将其包装在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(); 
    } 
}