2015-11-16 87 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(); 
    } 
}