3

During code review I had the following question raised and I didn't know why java didn't complain about an unhandled exception in my code. Here is the code in question that doesn't need a "throws Exception" in the method signature:

private void foo() {
    try {
        String s = "Hello, world.";
        return;
    } catch (Exception ex) {
        throw ex;
    }
}

Here is the code that would show an unhandled exception message unless I declare a "throws Exception" in the method signature.

private void bar() throws Exception {
    try {
        String s = "Hello, world.";
        return;
    } catch (Exception ex) {
        throw new Exception();
    }
}

Why does bar() require a throws keyword in the method signature and foo() doesn't? They both are throwing Exception and they are both "handled" by the catch clause.

****EDIT****

The link to the other question isn't helpful to me yet. Could someone explain how that answer is relevant to this one? Thanks.

PeonProgrammer
  • 1,445
  • 2
  • 15
  • 27

0 Answers0