2012-10-25 45 views
1

我正在創建一個用於整數的通用Pair類。需要幫助來捕捉異常

Exception類:

public class WrongThing extends Exception{ 
    public WrongThing(String message) { 
     super(message); 
    } 
} 

主要類:

public class Pair<E> implements Comparable<E>{ 
    private E var1; 
    private E var2; 
    public Pair(E var1, E var2) throws WrongThing{ 
     //this is on purpose!!! 
     System.out.println("var1 and var2 are switched"); 
     this.var1 = var2; 
     this.var2 = var1; 
    } 

    void get(){ 
     System.out.println("the first actualy 2nd: "+ 
       var1 + "the "); 
       System.out.println(" second actualy first" + var2); 
    } 

    void set1(E temp){ 
     System.out.println("var1 and var2 are switched"); 
     temp = var1; 
    } 

    void set2(E temp){ 
     System.out.println("var1 and var2 are switched"); 
     temp = var2; 
    } 

    E smallest(E var1, E var2){ 

     return var1; 
    } 

    @Override 
    public int compareTo(Object arg0) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 
} 

測試用例

import java.util.Scanner; 
import java.util.InputMismatchException; 

public class PairTest { 
    public static void main(String[] args) throws WrongThing{ 
     System.out.println("two integers please"); 
     Scanner sc = new Scanner(System.in); 
     Pair<Integer> newPair; 
     Pair<Integer> tempPair1= new Pair<Integer>(3,2); 

     try{ 
      newPair = new Pair<Integer>(sc.nextInt(),sc.nextInt()); 
      //throw new InputMismatchException("that is not an Integer...."); 
     }catch(WrongThing exception){ 
      //System.out.println("you cant do that. try again and press enter after the first integer"); 
      newPair = new Pair<Integer>(sc.nextInt(),sc.nextInt()); 
      newPair.get(); 

     } 
     finally{ 


     } 
    } 
    //newPair = new Pair<Integer>(sc.nextInt(),sc.nextInt()); 
} 

當我運行這段代碼,我得到一個InputMismatchException。我沒有正確創建我的異常,或者在拋出異常時沒有捕獲異常嗎?

+0

我已經刪除了Eclipse標籤,因爲這個問題與您使用的IDE無關:這是一個「純粹的」Java問題。 –

回答

1

除非我錯過了一些東西,它看起來像你只是想趕上WrongThing異常,而不是InputMismatchEception

try{ 
    // ... 
}catch(WrongThing exception){ 
    // ... 
} 
1

WrongThing是您的自定義異常。必要時您需要扔掉並抓住它。

try{ 
    // throw new WrongThing("wrong thing"); 
}catch(WrongThing e1){ 
    // ... 
}catch(InputMismatchException e2){ 
    // ... 
} 

InputMismatchExceptionsc.nextInt()方法if the next token does not match the Integer regular expression, or is out of range拋出。所以你也需要緩存它。

0

對於你必須遵循的過程hasNext Scanner類() - > next()的

你不會得到這樣的任何異常。

Scanner sc = new Scanner(System.in);  
      while (sc.hasNext()) { 

       if(sc.hasNextInt()) 
       System.out.println(sc.nextInt()); 

       sc.next(); 
      } 
     sc.close(); 

的另一件事是有異常塊與catch(Exception e)級聯底部,以確保捕獲所有異常。

try{ 
     // code which throws checked exceptions. 
     }catch(WrongThing e1){  
      e1.printStackTrace(); 
     }catch(Exception e2){ 
      e2.printStackTrace(); 
     }finally{ 
      // cleanup 
     } 
0

java.util.InputMismatchException文檔說:

Scanner拋出,表明檢索到的令牌不期望類型匹配的模式,或者該標記超出範圍的預期類型。

的一個用於Scanner.nextInt()說:

拋出:InputMismatchException - 如果下一個標記不匹配Integer正則表達式,或超出範圍

基本上例外正被當您在控制檯中鍵入非整數的內容時從Scanner.nextInt()拋出。當發生這種情況時,構造函數new Pair<Integer>(...)甚至不會被調用,所以您將它放入構造函數中的任何檢查都將變得毫無用處。

你需要做的是

newPair = null; 
while (newPair == null) { 
    try { 
     newPair = new Pair<Integer>(sc.nextInt(),sc.nextInt()); 
    } catch(InputMismatchException exception){ 
     System.out.println("you cant do that. try again and press enter after the first");    
    } 
} 
0

當你創建一類

Pair newPair = new Pair<Integer>(sc.nextInt(),sc.nextInt()); 

的對象作爲你所提到的在你的類這種類型的異常,只能拋出定製異常。 sc.nextInt()預期來自控制檯的Integer類型的輸入,並且當任何其他字符通過時,它會拋出InputMismatchException。因此,您還需要捕獲InputMismatch異常,例如

try{ 
    // throw new WrongThing("wrong thing"); 
}catch(WrongThing e1){ 
    // ... 
}catch(InputMismatchException e2){ 
    // ... 
} 
0

掃描器正在拋出InputMismatchException,您不處理這個異常。 InputMismatchException是一個RuntimeException,所以你不需要明確地捕獲它。 (通常TESE是程序員的錯誤)。如果您使用的是Java 7,那麼你可以使用這個語法(multicatch)來處理多個異常

try{ 
.... 
} 
catch(WrongThing|InputMismatchException e1){ 
     //deal with exception. 
} 

這是假設你想要做處理在同兩個異常辦法。如果你不這樣做,我會建議把它們分開,因爲其他答案已經清楚了。