I searched for a potential answer to my question below and did not find it.
I understand the difference between checked and unchecked exceptions, as well as what a programmer can / should do with them. However, I don't quite understand how the compiler interprets unchecked exceptions when it comes to interfaces.
Recently, I have been 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 where I am confused. The following implementation will also be implemented:
public class Divide implements Operation { @Override public int operate(int x, int y) { return x / y; } }
Why doesn't it matter to the compiler that I declared the method as an exception exception in the interface? I understand that Unchecked Exceptions are Runtime Exceptions exceptions and that the programmer does not need to handle them, but I donβt understand why, when I explicitly specify in my interface that I want the processed exception not to force me to handle it.
Can someone give an explanation why the compiler will resolve this situation?
Zach
source share