I've searched for a potential answer to my question below, and have not found one.
I understand the difference between checked and unchecked exceptions as well as what the programmer can/must do with them. However, I don't quite understand how the compiler interprets unchecked exceptions when it comes to interfaces.
Recently, I was programming an interface and implementation similar to the following:
public interface Operation {
int operate(int x, int y) throws ArithmeticException;
}
public class Divide implements Operation {
@Override
public int operate(int x, int y) throws ArithmeticException {
try {
return x / y;
} catch (ArithmeticException ex) {
System.out.println("Division error!");
}
}
}
Here's where I'm confused. The following implementation will also compile:
public class Divide implements Operation {
@Override
public int operate(int x, int y) {
return x / y;
}
}
Why does the compiler not care that I have declared the method as throwing an exception in the interface? I understand that Unchecked Exceptions are Runtime Exceptions and that it is not required that the programmer handle them, but do not understand why when I explicitly state in my interface that I want the exception handled that it does not force me to handle it.
Could someone lend an explanation as to why the compiler will allow this situation to occur?