-1

I have been using Find Bugs in Eclipse and I can not figure out why some of the bugs are coming up or how to fix them. Any ideas or help would be great!

The first bug is (Bug: Exception is caught when Exception is not thrown in banking.primitive.core.ServerSolution.saveAccounts()):

} catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();

The second bug is (Bug: Exception is caught when Exception is not thrown in banking.primitive.core.ServerSolution.saveAccounts()):

out.writeObject(accountMap.get(i));

I tried to change it to :

out.writeObject(accountMap.get(Integer.toString(i)));

The third bug is (Bug: Exception is caught when Exception is not thrown in banking.primitive.core.ServerSolution.saveAccounts()):

        } catch (Exception e) {
        e.printStackTrace();
        throw new IOException("Could not write file:" + fileName);

For the first bug this is with my try block as well. I am lost. I tried to follow you post below, but I am confused. Sorry, I am very new!

    public ServerSolution() {
    accountMap = new HashMap<String,Account>();
    File file = new File(fileName);
    ObjectInputStream in = null;
    try {
        if (file.exists()) {
            System.out.println("Reading from file " + fileName + "...");
            in = new ObjectInputStream(new FileInputStream(file));

            Integer sizeI = (Integer) in.readObject();
            int size = sizeI.intValue();
            for (int i=0; i < size; i++) {
                Account acc = (Account) in.readObject();

                //CST316 TASK 1 CHECKSTYLE FIX
                if (acc != null) {
                    accountMap.put(acc.getName(), acc);
                }
            }
        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
    }
}
user3320746
  • 7
  • 1
  • 3
  • What checked exceptions are thrown by methods called in the `try` block? Catch those *specifically*. Also catch `RuntimeException`, if you want. – Andy Turner Feb 15 '16 at 22:54

1 Answers1

0

See FindBugs Bug Description:

This method uses a try-catch block that catches Exception objects, but Exception is not thrown within the try block, and RuntimeException is not explicitly caught. It is a common bug pattern to say try { ... } catch (Exception e) { something } as a shorthand for catching a number of types of exception each of whose catch blocks is identical, but this construct also accidentally catches RuntimeException as well, masking potential bugs.

A better approach is to either explicitly catch the specific exceptions that are thrown, or to explicitly catch RuntimeException exception, rethrow it, and then catch all non-Runtime Exceptions, as shown below:

try {
    ...
} catch (RuntimeException e) {
    throw e;
} catch (Exception e) {
    ... deal with all non-runtime exceptions ...
}
dur
  • 15,689
  • 25
  • 79
  • 125