Emmanuel Lecharny schrieb: > Hi guys, > > I'm facing an interesting question here : should constructors throw > checked exceptions? > > I just feel like I don't like the idea at all, but may be I'm wrong... > > The problem is that for the new ServerEntry API, I have to guarantee > that the constructors are called with valid parameters. There are many > possible options : > 1 - use Assert in the constructors (but they will be disabled by > default if you don't add the -ea in the JVM command line) > 2 - use Checked Exception (but then the caller will have to > try...catch the code > 3 - use Runtime Exception > 4 - don't do anything in the constructors, but handle the controls in > setters > 5 - just consider the user won't use the constructors with bad parameters > 6 - default to a base object if inside the constructor, we have some > exception > > This is not clear to me what should be the best solution. ATM, I > incline to think that solution 3 is the less offending.
+1 for 3 |What about java.lang.IllegalArgumentException? It says precisely what's the problem (and fits to your needs) and is a Subclass of RuntimeException?| My 2 cents Felix
