Java 试一试catch dones';无法捕捉异常
在下面的代码中,我得到了IOException的Java 试一试catch dones';无法捕捉异常,java,try-catch,ioexception,Java,Try Catch,Ioexception,在下面的代码中,我得到了IOException的不可访问的catch块。在}catch(IOException e){中使用IOException时,决不会从try语句体错误(带下划线)引发此异常 class driver { public static void main(String[] args) { // TODO Auto-generated method stub Customer forrest = new Customer("Forrest Gump", 1
不可访问的catch块。在}catch(IOException e){
中使用IOException
时,决不会从try语句体错误(带下划线)引发此异常
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
块中所做的任何操作都不会引发IOException
,这是因为您在try/catch
中执行的任何操作都不会引发IOException
根据扫描仪
大多数Scanner类方法抛出FileNotFoundException
(这在您的情况下不适用,因为不从文件中读取)(或)IllegalArgumentException
(或)IllegalStateException
您需要将IOException
更改为上述任一异常(或)删除try/catch。该try块中的任何方法调用都不会抛出IOException
,这就是catch是无法访问的代码的原因
您可以安全地删除try和catch,因为这种情况永远不会发生。您的catch
块是空的,这表明您无论如何都不会真正处理异常。您可能在其中有一些代码使您捕获了它,但现在您不再拥有它。只需删除整个try-catch
将不会有进一步的问题。您可以删除IOException和try-catch,因为try-catch中的任何语句都不会引发此异常。try/catch
块为空,代码不会引发IOException
,但它可能会引发其他异常。您对源代码做了什么要删除缩进?我下一步一定要这样做time@assylias:同意。更新答案。