user133466
user133466

Reputation: 3415

Why try-catch doesn't catch IOException?

In the following code I get the Unreachable catch block for IOException. This exception is never thrown from the try statement's body, the error(underlined) with the IOException in } catch (IOException e){.

What am I doing wrong?

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

  }
}

Upvotes: 2

Views: 8008

Answers (6)

kosa
kosa

Reputation: 66637

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.

Upvotes: 11

Roman C
Roman C

Reputation: 1

try/catch block is empty and code is not throw IOException but it may throw other exceptions.

Upvotes: 1

CodeDreamer
CodeDreamer

Reputation: 444

You can remove the IOException and the try catch since none of the statements inside the try catch is throwing this exception

Upvotes: 4

Marko Topolnik
Marko Topolnik

Reputation: 200148

Your catch block is empty, indicating that you weren't really going to handle the exception, anyway. 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.

Upvotes: 3

Guido
Guido

Reputation: 47665

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.

Upvotes: 3

Nick Rolando
Nick Rolando

Reputation: 26167

Seems like nothing you are doing in that try block is throwing the IOException

Upvotes: 3

Related Questions