[英]try-catch doesn't catch IOException
In the following code, I get the Unreachable catch block for IOException
. 在下面的代码中,我获得了Unreachable catch block for IOException
的Unreachable catch block for IOException
。 This exception is never thrown from the try statement body error(underlined) with the IOException
in } catch (IOException e){
what am I doing wrong? 永远不会从try语句体错误(带下划线)抛出此异常,其中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);
}
}
It is because none of the operations you perform inside try/catch
throws IOException. 这是因为你在try/catch
执行的操作都没有抛出IOException。
As per Scanner javadoc 根据Scanner javadoc
Most of the Scanner class methods throws either FileNotFoundException
(Which is not applicable in your case because not reading from file) (or) IllegalArgumentException
(or) IllegalStateException
大多数Scanner类方法抛出FileNotFoundException
(由于不从文件中读取,因此不适用于您的情况)(或) IllegalArgumentException
(或) IllegalStateException
You need to change IOException
to either of above exceptions (or) remove try/catch. 您需要将IOException
更改为上述任一异常(或)删除try / catch。
您可以删除IOException和try catch,因为try catch中的所有语句都不会抛出此异常
在try
块中抛出IOException
似乎没什么
No method call in that try block throws a IOException
, that is why the catch is an unreachable code. 在try块中没有方法调用抛出IOException
,这就是catch是无法访问的代码的原因。
You can safely remove both the try and the catch, as that situation will never happen. 您可以安全地删除try和catch,因为这种情况永远不会发生。
Your catch
block is empty, indicating that you weren't really going to handle the exception, anyway. 你的catch
块是空的,表明你无论如何都不会真正处理异常。 You probably had some code in there that made you catch it, but now you don't have it anymore. 你可能在那里有一些代码让你抓住它,但现在你已经没有它了。 Just remove the entire surrounding try-catch
and there will be no further problems. 只需删除整个周围的try-catch
,就不会有其他问题。
try/catch
块为空,代码不会抛出IOException
但它可能抛出其他异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.