4

在下面的代码中,我得到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);

  }
}
4

6 回答 6

11

It is because none of the operations you perform inside try/catch throws IOException.

As per 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

You need to change IOException to either of above exceptions (or) remove try/catch.

于 2012-11-13T21:07:22.600 回答
4

您可以删除 IOException 和 try catch,因为 try catch 中的任何语句都不会引发此异常

于 2012-11-13T21:14:04.397 回答
3

No method call in that try block throws a IOException, that is why the catch is an unreachable code.

You can safely remove both the try and the catch, as that situation will never happen.

于 2012-11-13T21:07:25.117 回答
3

似乎您在该try街区所做的任何事情都没有抛出IOException

于 2012-11-13T21:06:58.140 回答
3

您的catch块是空的,表明您无论如何都不会真正处理异常。你可能有一些代码让你捕捉到它,但现在你不再拥有它了。只需移除整个周围try-catch,不会有进一步的问题。

于 2012-11-13T21:12:09.467 回答
1

try/catch块是空的,代码不会抛出IOException,但它可能会抛出其他异常。

于 2012-11-13T21:22:45.217 回答