无法确定用户为自定义异常输入小于0的数字的问题

时间:2014-04-07 03:32:45

标签: java try-catch custom-exceptions

似乎无法想象这一点。如果用户输入无效余额,我如何重新提示他们再次输入余额并继续我的程序?

  //EDITED STILL WONT WORK PROPERLY
 boolean again;
    while(again = true) 
    {
        try {

            // pass object to printwriter and pw to write to the file
            pw = new PrintWriter(fw);

            System.out.print("Input beginnning balance: ");
            balance = input.nextDouble();
            again = false;
            // pass user input to object
            AccountWithException acctException = new AccountWithException(fullName, balance, id, RATE);
            again = false;

            System.out.println(acctException.toString());

            // copy object to created file
            pw.println(acctException.toString());
            again = false; 

        // custom exception
        } catch (InvalidBalanceException e) {
            System.out.println(e.getMessage());
        } catch(FileNotFoundException e) {
            System.out.println(e.getMessage());
        } finally {
            pw.close();

3 个答案:

答案 0 :(得分:1)

你可以抛出Invalidbalanceexception并在catch块中捕获它,就像这样

try {

        // pass object to printwriter and pw to write to the file
        pw = new PrintWriter(fw);

        // pass user input to object
        AccountWithException acctException = new AccountWithException(fullName, balance, id, RATE);

        System.out.println(acctException.toString());

        // copy object to created file
        pw.println(acctException.toString());
throw new InvalidBalanceException ();

        // custom exception if balance < 0
    } catch (InvalidBalanceException e) {
        System.out.println(e.getMessage());
        System.out.println("Re-enter balance: ");
        balance = input.nextDouble();
    } catch(FileNotFoundException e) {
        System.out.println(e.getMessage());
    } finally {
        System.out.println("Text file closed, program complete...");
        pw.close();
    }

答案 1 :(得分:0)

do{
    //take user input
    //check for invalid balance
    try{
        if(balance is invalid){
            throw new InvalidBlanceException();
        }
    } catch(InvalidBalanceException e) {
        //user input again reprompt
    }
}while(end of file)

答案 2 :(得分:0)

一个问题是你的while循环。这项任务不会有助于评估。 此外,如果抛出异常,它将触发catch块。

 boolean again = true;
    while(again) // check for true instead of making assignment 
    {
        try {

            // pass object to printwriter and pw to write to the file
            pw = new PrintWriter(fw);

            System.out.print("Input beginnning balance: ");
            balance = input.nextDouble();
            // pass user input to object

            // throw an exception to trigger catch block
            // according to catch block, should be an InvalidBalanceException
            if(balance < 0) throw new AccountWithException(fullName, balance, id, RATE);
            again = false; // this line isn't run if it fails to exception

        // custom exception
        } catch (InvalidBalanceException e) {
            System.out.println(e.getMessage());

            // copy object to created file
            pw.println(e.toString());
        } catch(FileNotFoundException e) {
            System.out.println(e.getMessage());
        } finally {
            pw.close();