使用 Eclipse 在 java 中抛出自定义异常和 {} 问题

Throwing custom exception and problems with {} in java with eclipse

我正在尝试为我的应用程序创建自定义例外。它生成一个随机数,从 BufferedReader 中取出一个字符,通过枚举将该字符与一个 int 相关联,并输出该 int 是否高于、低于或等于随机 int。然后就可以玩了

我有三个错误。在我的两个自定义异常 classes 中,我都在 class 正文 { 上收到删除标记 '{',{ 错误。然后对于我的 BufferedReader,.readline() 方法抛出 IOException。所以我的自定义异常 NewIOException 扩展了 IOException 并添加了一个 try、catch 和 finally 块来捕获它。我收到错误 "Unhandled Exception type 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;
    }
}

这些是我对例外 classes 的编辑。我对 Guess.userInput() 的调用需要在两个异常 classes 中抛出 StringLengthException,我不知道这是否是正确的举动。我的异常 classes 中的 extends 也有错误,Guess.java 的 throws 子句中的逗号以及紧接其下方的 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();
    }
}

此代码:

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

你在另一个异常中有同样的错误 class - StringLengthException.

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

您的代码中的另一个问题 - 当您有 catch (NewIOException e) 时,该子句不会捕获 IOException 而非 NewIOException。因此,您应该有一个 catch (IOException e) 子句。实际上,我没看到你在哪里抛出 NewIOException,所以你应该只捕获 IOException.

您的捕获块可能如下所示:

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

另请注意,如果您捕捉到异常 X,则您的方法签名中不需要 throws X 子句。您要么捕获异常,要么声明您的方法 throws 它。

我注意到的另一个错误:

从异常 classes 中删除 throws 子句。该子句仅在方法中使用。