Question

Suppose I have a class ClassA whose getters and setters I use multiple times in my program. Suppose at the end of the program it is critical that I run the public void checkErrors() within the instance of ClassA that I've been working with, which will throw an exception or log an error if there is an error.

Is there a way I can set up ClassA that will throw a RuntimeException if checkErrors() isn't called at least once by the main method?

For example, hoping that you have an imagination, it would be good to have a method like: public compulsory void checkErrors() where compulsory means it must be run once by the main method.

This is an event driven programming context.

In my problem, the instance of ClassA might be needed for a few calculations then not needed any more (with the state of the class, resulting from various internal calculations and setters, being used in other parts of the program). Thus, at the end of the instance's life in the program, I would like to checkErrors() before continuing the program (thus stopping error propagation to later stages of the program).

This compulsory thing would just be to stop the error on behalf of the programmer to call this method at least once.

Was it helpful?

Solution

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.

OTHER TIPS

What you are requesting goes against the principle of object oriented programming, as individual classes should encapsulate specific behaviour, without having to be concerned with the flow of the whole program. The class itself shouldn't be aware, that something was supposed to happen with it, it should have been instantiated, etc., this should be monitored outside of the class, in the main flow.

Therefore if you need to do this badly, you can add a counter to ClassA that counts, how many times something was performed and then check this counter in the main.

Sure what you are talking about is the validity of the data in your class. At some point the data is valid. So you need to check the validity of your data every time it is changed and maintain the state of this validity.

private boolean isValid;

public void setSomeData() {
  // set it
  checkIfErrors();
}

public Data getSomeData() {
  if (isValid) {
    return someData;
  } else {
    throw new DataIsInvalidException();
  }
}

private void checkIfErrors() {
  if (isOk) {
    isValid = true;
  }
}

There's no certain way to guarantee that an object's method is called after it's constructed. You can, however, require that a method be called before another method; this pattern can be used to enforce a "lifecycle", for example.

boolean errorChecked = false;

public void checkForErrors() {
    // do stuff and throw exception if there's a problem
    errorChecked = true;
}

public void doSomething() {
    if(!errorChecked)
        throw new IllegalStateException("must call checkForErrors() first");

    ...
}

While this paradigm is not really suited for the purposes you are seeking, you could try something like this. Create a class Monitor:

public class Monitor{

    private static ArrayList<Object> checkList;

    public static void addToList(Object a){ // implementation }
    public static void removeFromList(Object a){ // implementation }
}

This class could have a timer thread that checks its contents every so often. Then you can build right into the class you need checked:

public class ClassA{

    public ClassA(){

        Monitor.addToList(this);
        ...
    }

    public void checkErrors(){

        Monitor.removeFromList(this);
        ...
    }

}

So all the timer needs to do is look at the list every so often and see if any of the objects in it are null. If one is null, something was freed without a call to checkErrors(), and you can throw your exception.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top