I have 2 objects from two different classes sharing an attribute of common type (IpAddress which contains all information about the subnet as well). Now, IpAddress is a user define type and I have a function setSecondaryRouter() which can mutate a property of this type. To make the program run as expected I need an invariant (or 2 invariants really) to hold. This is checked in the function setSecondaryRouter(). However, I want to set this property secondaryRouter to be the same in the second object as the first. This kind of implies that the invariant already holds the second time I use setSecondaryRouter(). Due to this I know that the function will not throw.
The problem is that my compiler "helps" to ensure that I do not have uncaught exceptions by not compiling unless I add a try-catch clause or add a throws declaration the caller function (thus helping to solve a non existing problem). What I want to do is to tell Java that this exception will not be thrown. Compare this with the nothrow keyword in c++. I know similar questions are already posted, like the following
Is there any standard annotation to indicate nothrow semantics in Java?
But my question differ in that I do not want to add a throw, or try-catch clause to specific exceptions which I know will not be thrown!
Also one of the expressions is an IllegalArgumentException. Can this exception be the reason that I have to handle the exceptions?