gpt4 book ai didi

java - try-catch 不捕获 IOException

转载 作者:搜寻专家 更新时间:2023-11-01 01:00:18 24 4
gpt4 key购买 nike

在下面的代码中,我得到了 Unreachable catch block for IOException。在 } catch (IOException e){ 我做错了什么?

class driver {

public static void main(String[] args) {
// TODO Auto-generated method stub
Customer forrest = new Customer("Forrest Gump", 1,
"42 New Street, New York, New York");
Customer random = new Customer("Random Name", 2,
"44 New Street, New York, New York");
Customer customer[] = { null, forrest, random };
int whichOption = 1;
int id = 0;
char action = ' ';
char accSrc = ' ';
char accDest = ' ';
double amount = 0;
BankAccount src = null;
do {

try{
// process JOptionPane input information
String input = JOptionPane
.showInputDialog("Please enter your transaction information: ");
Scanner s = new Scanner(input);
id = Integer.parseInt(s.next());
action = Character.toUpperCase((s.next().charAt(0)));

if (action == 'T') {
amount = s.nextDouble();
accSrc = s.next().charAt(0);
accDest = s.next().charAt(0);
} else if (action == 'G' || action == 'I') {
accSrc = s.next().charAt(0);
} else {
// if D,W
amount = s.nextDouble();
accSrc = s.next().charAt(0);
}

} catch (IOException e){

}
// taking action accordingly (T)ransfer, (D)eposit, (W)ithdraw, (I)nterest
if (action == 'T') {

(customer[id].getAccount(accSrc)).transfer(amount,
customer[id].getAccount(accDest));
} else if (action == 'G') {
System.out.println("The current balance on your " + accSrc
+ " account is "
+ customer[id].getAccount(accSrc).getBalance() + "\n");
} else if (action == 'D') {
(customer[id].getAccount(accSrc)).deposit(amount);
} else if (action == 'W') {
(customer[id].getAccount(accSrc)).withdraw(amount);
} else if (action == 'I') {
(customer[id].getAccount(accSrc)).computeInterest();
}
whichOption = JOptionPane
.showConfirmDialog(null , "Do you want to continue?");

System.out.println("The balance on " + customer[id].getName()
+ " auto account is " + customer[id].A.balance);
System.out.println("The balance on " + customer[id].getName()
+ " savings account is " + customer[id].S.balance);
System.out.println("The balance on " + customer[id].getName()
+ " checkings account is " + customer[id].C.balance);
System.out.println("The balance on " + customer[id].getName()
+ " loan account is " + customer[id].L.balance + "\n");

} while (whichOption == 0);

}
}

最佳答案

这是因为您在 try/catch 中执行的所有操作都不会抛出 IOException。

根据扫描器 javadoc

大多数 Scanner 类方法抛出 FileNotFoundException(这不适用于您的情况,因为不是从文件读取)(或)IllegalArgumentException(或)非法状态异常

您需要将 IOException 更改为上述任一异常(或)移除 try/catch。

关于java - try-catch 不捕获 IOException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13368765/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com