Java: Appropriate exception for initialization error
Solution 1
If you are throwing an exception in a Factory due to insufficient data, I like to throw an IllegalStateException
with a description similar to "cannot construct X, no Y has been set".
If you are throwing an exception in a Factory due to conflicting data, I like to throw an
IllegalStateException
with a description similar to "cannot construct X, Y conflicts with Z".
If you are throwing an exception in a Factory due to a bad (or nonsensical) value, I like to throw an IllegalArgumentException
with a description similar to "Y cannot be A".
If you are throwing an exception in a Factory due to a missing value, I like to throw an IllegalArgumentException
with a description similar to "Y cannot be null".
The last preference is up to some debate. Some people suggest that it might be better to throw a NullPointerException
; in my case, we avoid them at all costs since many customers tend to not read the exception message (and assume that NullPointerException means a coding error).
In any event, you should provide a good, specific, message as to why the exception was thrown, to ease your future support costs of seeing that exception raised a few months from now.
Solution 2
You can create your own Exception by extending Exception class
Solution 3
Something like this probably should just be an Assert, but if there is in fact the possibility for this to fail then a custom exception that is meaningful to you would be my choice.
Solution 4
Yeah, the cause of the problem is your best bet. If the arguments are not OK, you can throw IllegalArgumentException, if some file is not there, you can throw FileNotFoundException, if the factory is not initialized properly, you can throw IllegalStateException, etc., etc...
However, creating your own Exception is easy. Just declare you class as extends Exception
and add delegate constructors. If you extend Exception, then that method which can throw it must be declared with throws
. If you don't want that, you can extend RuntimeException, those need not be declared.
Solution 5
Ideally you would like to extend Exception
and craft your own IntializatonException
.
Adam Matan
Team leader, developer, and public speaker. I build end-to-end apps using modern cloud infrastructure, especially serverless tools. My current position is R&D Manager at Corvid by Wix.com, a serverless platform for rapid web app generation. My CV and contact details are available on my Github README.
Updated on March 14, 2020Comments
-
Adam Matan about 4 years
Which exception should I throw when a static factory method fails to initialize a new object? I prefer raising a meaningful exception rather than returning
null
.-
Viruzzo about 12 yearsIt would depend on what caused the failure itself, i.e. a network problem, a file read failure, etc..
-
Adam Matan about 12 yearsYou mean I should just throw whatever exception I got during initialization?
-
Viruzzo about 12 yearsNot necessarily, but your exception should be meaningful in telling what went wrong, not just that something went wrong.
-
-
Adam Matan about 12 yearsIsn't there a built-in exception for this case? It's quite a common scenario.
-
Woot4Moo about 12 yearsI would hardly call a constructor throwing an exception a common scenario
-
Rahul Borkar about 12 yearsThere are lot of Exceptions but it is more flexible to have your own if customized message needs to be displayed