I want make it compulsory for the main method to checkErrors() on ClassA before ClassB uses ClassA's state in an event driven programming case.
Then you shouldn't design the program this way. Instead, you should introduce a class ValidState
and pass an instance of this class to ClassB
. And ClassA
should have a method
ValidState produceValidState() throws IllegalStateException
which would check for errors and, if none, produce the valid state.
That way, it's completely impossible for the main method to pass invalid state to ClassB. It has to ask ClassA to check for errors in order to get the valid state that ClassB needs to work.