java无法确定用户何时为我的自定义异常输入数字小于0的问题
我似乎无法理解这一点。如果用户输入了无效的余额,我如何重新命令他们再次输入余额,并继续我的程序
//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();
# 1 楼答案
一个问题是你的while循环。这项作业对评估没有帮助。 此外,如果抛出异常,它将触发catch块
# 2 楼答案
# 3 楼答案
您可以抛出
Invalidbalanceexception
,然后像这样在catch块中捕获它