1

I have a class like below:

public class MyClass {

    public void foo() throws IOException {
        System.out.println("Hola");
    }

    public MyClass() throws IOException {

    }

}

As you can see I am declaring IOException in the throws clause of the method and the constructor. But I am not throwing that exception anywhere in the body. So it should be a compile time error like when we try to catch an exception that is not being thrown from the try block. But in this case it compiles fine. Can anyone explain the reason behind this behavior?

Sattyaki
  • 376
  • 2
  • 9
  • Possible duplicate of [Java: checked vs unchecked exception explanation](https://stackoverflow.com/questions/6115896/java-checked-vs-unchecked-exception-explanation) – shinjw Aug 17 '18 at 05:22

3 Answers3

1

Declaring throws IOException does not require that you actually throw the exception.

If that were the case, then it would be impossible to program because all branches of the method would be required to throw the exception (even in non-exceptional cases).

This is more a question of having complete contracts, where the caller is enabled to handled possible exceptions. This accommodates future implementations that may be forced to actually throw the exception.
It's probably for the same reason that overridden methods are allowed to omit checked exceptions (you are not forced to throw it)

ernest_k
  • 44,416
  • 5
  • 53
  • 99
0

Show Stopper has a great answer and references a great post. This answer is an attempt to help OP come to an understanding on what checked and unchecked exceptions are.

Checked exceptions are an exceptions declared in the signature (whether thrown or not).

public void doSomething() throws SomeExtremelyDevastatingEndOfTheWorldException {
    // Maybe it might happen (but whoever calls this MUST acknowledge handle this case)
}

This application will not compile because the author deemed it necessary that this case must be handled for the application to run.

Unchecked exceptions are exceptions that don't necessarily need to be handled and is acceptable to bubble up at runtime.

public void doSomething() {
    throws RuntimeException("not the end of the world...but we'll probably want this stacktrace and let the program run on");
}

Now anything exception can become an unchecked exception if the author wishes it to be.

shinjw
  • 3,329
  • 3
  • 21
  • 42
-1

Declaring throws IOException does not require that method throw the exception.

It is part of method signature and also designing software. Consider below example. There is one method in Interface and two implementation of it. Both implementation should have same signature whether they are throwing exception or not. They can ignore exception clause. So this is valid reason why compiler can't show error while compilation.

Interface parent { 
    void method() throws Exception
}


Class Implementation1 {
    void method() throws Exception {
        System.out.println("Do not throws exception");
    }
}

Class Implementation2 {
    void method() throws Exception {
        throw new Exception("Error");
    }
}