2012-10-12 35 views
-3

我有問題。編碼如下所示。當我運行程序並輸入「aaa」時,它顯示錯誤,因爲它只捕獲算術異常。如何添加適當的代碼來克服基於問題發生的異常?程序趕上算術異常,並且將展示finally塊

import java.io.* ; 
public class FinallyPractice1 
{ 
    public static void main(String []) 
    { 
     BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in)); 
     String inData; int num=0, div=0; 
     try 
     { System.out.println("Enter the numerator:"); 
      inData=stdin.readLine(); 
      num=Integer.parseInt(inData); 

      System.out.println("Enter the divisor:"); 
      inData=stdin.readLine(); 
      div=Integer.parseInt(inData); 

      System.out.println(num+"/"+div+" is "+(num/div)); 
     } 
     catch(ArrayIndexOutOfBoundsException ae) 
     { 
     System.out.println("You can't divide "+ num + " by " + div); 
     } 
     catch(ArithmeticException aex) 
     { 
      System.out.println("You entered not a number: " + inData); 
     } 
     finally 
     { 
     System.out.println("If the division didn't work, you entered bad data."); 
     } 
      System.out.println("Good-by"); 
    } 
} 

enter image description here

我已經找到答案!編碼是象下面這樣:

import java.io.* ; 
    public class FinallyPractice1 
    { 
     public static void main(String [] a) throws IOException 
     { 
      BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in)); 
      String inData; int num=0, div=0; 
      try 
      { System.out.println("Enter the numerator:"); 
       inData=stdin.readLine(); 
       div=Integer.parseInt(inData); 

       System.out.println("Enter the divisor:"); 
       inData=stdin.readLine(); 
       div=Integer.parseInt(inData); 

       System.out.println(num+"/"+div+" is "+(num/div)); 
      } 
      catch(ArithmeticException ae) 
        { 
         System.out.println("ArithmeticException by " + div); 
        } 
        catch(ArrayIndexOutOfBoundsException ae) 
        { 
        System.out.println("You can't divide "+ num + " by " + div); 
       } 
        catch(NumberFormatException ae) 
        { 
        System.out.println("NumberException"); 
       } 
        finally 
        { 
        System.out.println("If the division didn't work, you entered bad data."); 
       } 
         System.out.println("Good-by"); 
     } 
    } 
+0

什麼碼例外......? –

+3

您忘記了粘貼代碼。點擊[編輯],並添加它。 – dasblinkenlight

+0

您是否介意加入你的源代碼?謝謝。 – reporter

回答

0

當你是故意輸入錯誤數據 「AAA」,您的發言:

div=Integer.parseInt(inData); 

將拋出一個NumberFormatException。您可以爲此添加一個catch塊:

... 
} catch (NumberFormatException nfe) { 
    System.err.println(nfe); 
    // more error handling 
} catch ... 
1

添加一個更catch塊,像這樣:

} catch(ArrayIndexOutOfBoundsException ae) { 
     System.out.println("You can't divide "+ num + " by " + div); 
    } catch(ArithmeticException aex) { 
     System.out.println("You entered not a number: " + inData); 
    } finally { 
     //.... 
    } 

Generaly,您可以根據需要儘可能多的catch塊添加到單個try塊。但要記得給他們正確的順序 - 首先放置更具體的例外,更通用 - 最後。

如果你想捕捉任何可能的例外,您可以使用Throwable類:

try { 
    // some potentially dangerous code 
} catch (Throwable th) { 
    // process error somehow 
} 
+0

我已經嘗試編譯和運行,但它表明標識預期的錯誤。 – Rebecca

+0

您是否導入了ArithmeticException類? – HitOdessit

+0

已經導入 – Rebecca

0

添加幾個catch blocks來處理異常。

catch(ArithmeticException ae) 
     { 
      System.out.println("ArithmeticException by " + div); 
     } 
     catch(ArrayIndexOutOfBoundsException ae) 
     { 
     System.out.println("You can't divide "+ num + " by " + div); 
     } 
     catch(IOException ae) 
     { 
     System.out.println("IOException"); 
     } 
     finally 
     { 
     System.out.println("If the division didn't work, you entered bad data."); 
     } 
      System.out.println("Good-by"); 
0

您可以使用新的multi-catch。它具有從

catch (ArithmeticException |ArrayIndexOutOfBoundsException ae) 

我會建議你抓你期待,而不僅僅是一條毯子的Throwable或異常