自定义异常类不在eclipse中打印消息

时间:2014-05-12 22:36:16

标签: java eclipse exception exception-handling custom-exceptions

我的课程已经设定好了。我希望捕获数字设置为下一个键盘整数的部分的异常。如果数字是1,我需要抓住。如果用户以单词形式输入数字。我创建了一个自定义异常类但我无法打印我的消息我收到一条空消息

主要代码:

import java.util.Scanner;


public class SuperLottoPlus {
public static void main(String[] args) throws LottoLimitException {

    Scanner keyboard = new Scanner(System.in);

    System.out.println("How many tickets do you want?");
    try {
        int number = keyboard.nextInt();
        for (int i = 0; i < number; i++)
            printTicket(generateNumber());
    } catch (Exception LottoLimitException) {
        System.out.println(LottoLimitException.getMessage());
    }

}
public static boolean exists(int [] array, int value)
{
    for (int i=0; i< array.length; i++)
    {
        if (array[i]== value)
            return true;
    }
    return false;
}

public static void printTicket(int [] array)
{
    for (int i=0; i< array.length-1; i++)
    {
        System.out.print(array[i] +" ");
    }
    System.out.println("MEGA: " + array[5]);
}


public static int[] generateNumber()
{
    int[] array = new int[6];

    int temp = (int) (46 * Math.random()) + 1;
    for (int i=0; i< array.length-1; i++)
    {
        while (exists(array, temp))
        {
            temp = (int) (46 * Math.random()) + 1;  
        }
        array[i] = temp;
    }
    array[5] = (int) (27 * Math.random()) + 1; 
    return array;
}
}

自定义异常代码

    public class LottoLimitException extends Exception {
    public LottoLimitException()
    {
        super("Sorry, there is a 1 lotto ticket minimum!");
        return; //should print when integer entered is less than 1
    }
    public LottoLimitException(String message)
    {
        super("Please enter a valid interger.");
        return; //should print when words are entered
    }
}

2 个答案:

答案 0 :(得分:1)

这应该得到你想要的东西

public static void main(String[] args) {

    Scanner keyboard = new Scanner(System.in);
    boolean valid = false;
    while(!valid) {
        System.out.println("How many tickets do you want?");

        try {
            int number = keyboard.nextInt();
            if(number < 1) throw new LottoLimitException();
            for (int i = 0; i < number; i++)
                printTicket(generateNumber());

            valid = true;
        } catch (LottoLimitException e) {  //number is less than 1
            valid = false;
            System.out.println(e.getMessage());
        } catch (Exception e) { //will get here for any other exception, such as invalid number
            valid = false;
            System.out.println("Invalid Format");
        }
    }
}

答案 1 :(得分:0)

您尝试捕获一个通用异常,将其称为LottoLimitException。远射,我想你需要的是:

public static void main(String[] args) throws LottoLimitException {

    Scanner keyboard = new Scanner(System.in);

    System.out.println("How many tickets do you want?");

    int number = keyboard.nextInt();
    if(number < 1) throw new LottoLimitException();
    for (int i = 0; i < number; i++)
        printTicket(generateNumber());
}

当然,你需要处理它。