I have a static Factory class that throws an exception if not inited before a call is made. What I don’t understand why eclipse is yelling at me for throwing the exception (it demands that I surround the toss in a try/catch) in the Factory, but in another class where I throw an UnsupportedOperationException, eclipse is just fine.
Is there a rule I am unaware of that I’m violating?
My static call (source of the issue):
public static Object createObject() {
if (CONTEXT == null)
throw new InstantiationException("Factory not inited.");
// ...
}
Edit: I realise this would probably be better suited as a singleton instead of a static class, but the question still stands.
You need to look at the difference between Checked exceptions and Unchecked exceptions.
http://docs.oracle.com/javase/tutorial/essential/exceptions/catchOrDeclare.html
If the exception you throw is derived from RuntimeException, it is Unchecked and doesn’t need to be declared/handled explicitly. All others must be.