2015-01-16 34 views
-2

我试图为我的应用程序创建自定义异常。它会生成一个随机数,从BufferedReader获取一个字符,通过一个枚举将该字符与一个int相关联,并输出int是否高于,低于或等于随机int。然后你可以再玩一次。在java中使用Eclipse投影自定义异常和问题

我有三个错误。在我的两个自定义异常类中,我都会得到一个删除标记'{',{class {body {。然后对于我的BufferedReader .readline()方法抛出一个IOException。所以我的自定义异常NewIOException扩展了IOException并添加了一个try,catch和finally块来捕获它。即使我的自定义异常扩展IOException,我也会收到错误“未处理的异常类型IOException”。我究竟做错了什么?我环顾四周,我没有看到任何照顾这件事。自定义异常扩展IOException并且我读过的自定义异常很短并且没有帮助。我有一本书“Learn Java for Android Development”,但代码示例并没有真正涵盖它。

这是我的代码。

package randomInt; 
import java.util.Random; 
import java.util.StringTokenizer; 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.lang.Character; 

public class Guess{ 
    int getNewRandom(){ 
     Random rand = new Random(); 
     int num = rand.nextInt(25); 
     return num; 
    } 
    public void userInput() throws StringLengthException, NewIOException'{ 
     System.out.println("Guess a letter between a and z: "); 
     int intRandom = getNewRandom(); 
     try{ 
      BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));` 
      String string= cin.readLine();  
      //StringTokenizer tokenizer = new StringTokenizer(string); 
      int length = string.length(); 
      if(length > 1){ 
       throw new StringLengthException(); 
      } 
      //String[] stringToToken = new String[numTokens]; 
      char guess = string.charAt(0);  
      char ch = Character.toLowerCase(guess); 
      //GuessDeterminer determine = new GuessDeterminer(ch); 
      int x = GuessDeterminer.determineGuess(ch); 
      if(x > intRandom){ 
       System.out.println("Your guess is too high."); 
       System.out.println("Choose again: "); 
       userInput(); 
      } 
      else if(x < intRandom){ 
       System.out.println("Your guess is too low."); 
       System.out.println("Choose again: "); 
       userInput(); 
      } 
      else if(x == intRandom){ 
       System.out.println("Your correct, congratulations!"); 
       System.out.println("Play again!"); 
       System.out.println("Guess a letter between a and z: "); 
       getNewRandom(); 
       userInput(); 
      } 
     } 
     catch (NewIOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     finally{ 
      System.out.println("Guess a letter between a and z: "); 
      getNewRandom(); 
      userInput(); 
     }  
    } 
} 


package randomInt; 

public class StringLengthException extends Exception{ 
    super("A new letter of length one from a - z must be chosen."); 
    super("Choose a letter: "); 
    Guess.userInput(); 
} 


package randomInt; 

import java.io.IOException; 

public class NewIOException extends IOException{ 
    super("Input Error"); 
    System.out.println("Input new letter from a - z: "); 
    Guess.userInput(); 
} 


package randomInt; 

public enum GuessDeterminer { 
    a('a'), 
    b('b'), 
    c('c'), 
    d('c'), 
    e('c'), 
    f('c'), 
    g('c'), 
    h('c'), 
    i('c'), 
    j('c'), 
    k('c'), 
    l('c'), 
    m('c'), 
    n('c'), 
    o('c'), 
    p('c'), 
    q('c'), 
    r('c'), 
    s('c'), 
    t('c'), 
    u('c'), 
    v('c'), 
    w('c'), 
    x('c'), 
    y('c'), 
    z('c'); 
    private final char character; 
    public final int returnAnswer; 
    GuessDeterminer(char character){ 
     this.character = character; 
     this.returnAnswer = determineGuess(character);  
    } 
    static int determineGuess(char ch){ 
     int x = 0; 
     switch(ch){ 
     case 'a' : x = 0; 
     case 'b' : x = 1; 
     case 'c' : x = 2; 
     case 'd' : x = 3; 
     case 'e' : x = 4; 
     case 'f' : x = 5; 
     case 'g' : x = 6; 
     case 'h' : x = 7; 
     case 'i' : x = 8; 
     case 'j' : x = 9; 
     case 'k' : x = 10; 
     case 'l' : x = 11; 
     case 'm' : x = 12; 
     case 'n' : x = 13; 
     case 'o' : x = 14; 
     case 'p' : x = 15; 
     case 'q' : x = 16; 
     case 'r' : x = 17; 
     case 's' : x = 18; 
     case 't' : x = 19; 
     case 'u' : x = 20; 
     case 'v' : x = 21; 
     case 'w' : x = 22; 
     case 'x' : x = 23; 
     case 'y' : x = 24; 
     case 'z' : x = 25; 
     } 
     return x; 
    } 
} 

这些是我对异常类的编辑。我对Guess.userInput()的调用需要在两个异常类中抛出StringLengthException,我不知道这是否正确。在我的异常类中,我也有错误,我的throws子句中的逗号为Guess.java,紧接着在println语句之下。这些错误的性质在下面的评论中解释。

public class StringLengthException extends Exception throws StringLenthException{ 
    public StringLengthException(){ 
     super("StringLengthException thrown."); 
     System.out.println("A new letter of length one from a - z must be chosen."); 
     System.out.println("Choose a letter: "); 
     Guess.userInput(); 
    } 
} 

public class NewIOException extends IOException throws StringLengthException{ 
    public NewIOException(){ 
     super("Input Error"); 
     System.out.println("Input new letter from a - z: "); 
     Guess.userInput(); 
    } 
} 
+0

您的'throws'声明中似乎有一个撇号。 – khelwood

+0

好吧,似乎照顾我的问题,同时摆脱NewIOException和只使用IOException。为什么我不能从NewIOException扩展IOException,并且覆盖IOException呢? – Spitz

回答

0

此代码:

super("Input Error"); 
System.out.println("Input new letter from a - z: "); 
Guess.userInput(); 

应该是在构造函数中:

public class NewIOException extends IOException{ 
    public NewIOException() { 
     super("Input Error"); 
     // though it's a bad practice to 
     // have this logic in the exception class 
     System.out.println("Input new letter from a - z: "); 
     Guess.userInput(); 
    } 
} 

你有同样的错误在你的其他异常类 - StringLengthException

但是,即使您修复了编译错误,异常也不应该包含处理该异常的代码。这就是catch块的用途。

代码中的另一个问题 - 当你有一个catch (NewIOException e)时,该子句不会捕获IOException而不是NewIOException。因此,您应该有一个catch (IOException e)子句。其实,我看不到你在哪扔NewIOException,所以你应该只抓到IOException

你的catch块可以是这样的:

catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    }  

还要注意的是,如果你赶上一个异常X,你不需要在你的方法签名throws X条款。你要么发现异常,要么声明你的方法throws它。

另一个错误,我注意到:

从您的异常类中删除throws条款。该子句仅用于方法中。

+0

我做了你的建议。这是一个很好的答案。但是,我添加了更多问题。用逗号分隔Guess.java中的userInput声明的异常时,出现此错误:此行有多个标记\t - 无效的字符常量\t - 令牌“,”,@的语法错误。然后在println的下一行我有这个错误:在这条线上的多个标记\t - 语法错误,插入“;”要完成BlockStatements - 语法错误,插入“AssignmentOperator Expression”来完成Expression - Syntax错误,插入“VariableDeclarators”来完成LocalVariableDeclaration。 – Spitz

+0

然后在extends关键字上的两个异常类中,我都有这样的错误:在这一行有多个标记 - 标记“{”,{在此标记之后预期的语法错误\t - 可序列化的类StringLengthException未声明类型的静态final serialVersionUID字段long - 在令牌“语法扩展”上出现语法错误,删除此令牌\t - 语法错误,插入“ClassBody”以完成ClassDeclaration - 语法错误,插入“}”以完成Block。我还将编辑添加到OP。 – Spitz

+0

@Spitz编辑答案 – Eran