![](/img/trans.png)
[英]Nested try-catch IOException inside catch for FileNotFoundException
[英]try-catch doesn't catch IOException
在下面的代碼中,我獲得了Unreachable catch block for IOException
的Unreachable catch block for IOException
。 永遠不會從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);
}
}
這是因為你在try/catch
執行的操作都沒有拋出IOException。
根據Scanner javadoc
大多數Scanner類方法拋出FileNotFoundException
(由於不從文件中讀取,因此不適用於您的情況)(或) IllegalArgumentException
(或) IllegalStateException
您需要將IOException
更改為上述任一異常(或)刪除try / catch。
您可以刪除IOException和try catch,因為try catch中的所有語句都不會拋出此異常
在try
塊中拋出IOException
似乎沒什么
在try塊中沒有方法調用拋出IOException
,這就是catch是無法訪問的代碼的原因。
您可以安全地刪除try和catch,因為這種情況永遠不會發生。
你的catch
塊是空的,表明你無論如何都不會真正處理異常。 你可能在那里有一些代碼讓你抓住它,但現在你已經沒有它了。 只需刪除整個周圍的try-catch
,就不會有其他問題。
try/catch
塊為空,代碼不會拋出IOException
但它可能拋出其他異常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.