2012-11-13 79 views
4

在下面的代碼中,我得到了Unreachable catch block for IOException。這個異常不會從try語句正文錯誤(帶下劃線)的IOException} catch (IOException e){中拋出,我做錯了什麼?try-catch不會捕捉到IOException

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); 

    } 
} 

回答

11

這是因爲沒有你的內心try/catch執行的操作拋出IOException異常。

按掃描儀javadoc

大多數掃描儀類的方法拋出要麼FileNotFoundException(是不是適合你的情況,因爲無法從文件中讀取)(或)IllegalArgumentException(或)IllegalStateException

您需要將IOException更改爲以上任一異常(或)刪除try/catch。

+0

FileNotFound是一個IOE ... – assylias

+1

@assylias:同意。更新了答案。 – kosa

3

好像什麼你正在做在try塊扔IOException

3

該try塊中沒有方法調用會拋出IOException,這就是爲什麼catch是無法訪問的代碼。

您可以安全地移除try和catch,因爲這種情況永遠不會發生。

3

您的catch塊爲空,表明您無論如何都不會處理異常。你可能在那裏有一些代碼讓你抓住它,但現在你不再有了。只需刪除整個周圍的try-catch,就不會再有問題了。

+0

你對源代碼做了什麼以去除凹痕?我會確保下次再做 – user133466

4

您可以刪除IOException異常,並由於沒有一個嘗試catch中的語句的嘗試捕捉拋出此異常

1

try/catch塊是空的,代碼不是扔IOException但它可能會引發其他異常。