使用eclipse在java中抛出自定义异常和问题

时间:2015-01-16 07:41:22

标签: java exception ioexception extends

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

我有三个错误。在我的两个自定义异常类中,我得到一个删除标记'{',{class body {的错误。然后,对于我的BufferedReader,.readline()方法抛出IOException。所以我的自定义异常NewIOException扩展了IOException并添加了try,catch和finally块来捕获它。我收到错误“Unhandled Exception type IOException”,即使我的自定义异常扩展了IOException。我究竟做错了什么?我环顾四周,我没有看到任何可以照顾的事情。自定义异常扩展IOException和我读过的自定义异常都没有什么是简短的,没有帮助。我有一本书“学习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,我不知道这是不是正确的举动。我的异常类中的扩展,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();
    }
}

1 个答案:

答案 0 :(得分: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的{​​{1}}。因此,您应该有一个NewIOException子句。实际上,我看不到你抛出catch (IOException e)的位置,所以你应该只抓住NewIOException

你的捕获块看起来像这样:

IOException

另请注意,如果捕获异常 catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } ,则方法签名中不需要X子句。您要么捕获异常,要么声明您的方法throws X

我注意到的另一个错误:

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