For people suggesting throwing an exception:
Throwing an exception does not give me a compile-time error, it gives me a runtime error. I know that I can throw an exception, it is better to die at compile time than at runtime.
I am using eclipse 3.4 first.
I have a data model that has a mode property, which is Enum.
enum Mode {on(...), off(...), standby(...); ...}
I am currently writing a representation of this model and I have code
... switch(model.getMode()) { case on: return getOnColor(); case off: return getOffColor(); case standby: return getStandbyColor(); } ...
I get the error "This method should return a result like java.awt.Color" because I have no default case and no xxx return at the end of the function. I want a compilation error in the case when someone adds another type to the enumeration (for example, shuttingdown), so I do not want to put the default case that generates AssertionError, because it will compile with the changed mode and will not be like an error before execution.
My question is this:
Why does EclipseBuilder (and javac) not recognize that this switch covers all features (or covers them?) And stops warning me about the need to return a type. Is there a way to do what I want without adding methods to the mode?
If this is not the case, is there a warning / error option in the switch statements that do not cover all possible Enum values?
Edit: Rob: This is a compilation error. I just tried to compile it using javac, and I get the "missing return statement" error message aimed at the last method. Eclispe simply puts the error at the beginning of the method.
java enums
KitsuneYMG May 13 '09 at 18:23 2009-05-13 18:23
source share