Dear experts (@dkayiwa, @mogoodrich, @mseaton, @darius, @wyclif )
I wanted to ask what the best practice is regarding exceptions in modules and get some input on my thoughts as well.
I want to clean up the API (all the OpenmrsService’s) of the radiology module in terms of its exceptions:
First the main rules I would apply (ideas are not mine, but from Joshua Bloch exceptional book Effective Java ):
- throw checked exceptions only when a user of the API would be able to recover from it given the exception and its additional information
- throw RuntimeException’s for programming errors/breach in the contract given by my API (for example if you want to get a RadiologyOrder by its UUID using RadiologyOrderService.getRadiologyOrderByUuid(String uuid) dont pass in NULL). And in my opinion better to throw a RuntimeException in such a case then to return null, so the consumer of the API quickly identifies where the error came from so he can fix his code.
By looking at openmrs-core, for example https://github.com/openmrs/openmrs-core/blob/2.0.x/api/src/main/java/org/openmrs/api/impl/OrderServiceImpl.java I can see that above points do apply.
Now my questions for checked exceptions in modules:
-
Should a module create its own general exception class like for example in the reporting module? Lets say mine would be called
RadiologyException
-
And if so should this exception be a sublcass of the openmrs.api.APIException?
-
Then if an exception is so special and needs some additional info I can create specialized exceptions as subclasses from
RadiologyException
? -
But then in the API I only use
RadiologyException
as checked exception, so client code is only forced to to handle this type, although my API methods can of course throw on of my specialized exceptions.