Reputation: 5544
Should a method (remote method call) return a boolean true
value indicating that an operation performed successfully even if all possible exceptions are thrown?
Example:
In my java application have many CRUD remote method calls and I catch all possible Exceptions and throw a Custom Exception to the calling client.
Should I now return void or a boolean, since the Exceptions already implicitly indicate the success or failure of the operation?
Upvotes: 3
Views: 1777
Reputation: 29493
Return void
, not a boolean
in this case.
Exceptions are for exceptional conditions. Why indicating something like the success or failure of an operation on two different channels? The DRY principle teaches us:
Don't repeat yourself
I would only use a boolean to indicate further information, like it is sometimes done on collections, reporting whether an item was found for removal.
Upvotes: 4
Reputation: 10093
I think this is a matter of convenience. The fact that you already throw a custom exception helps identify the exact error if there is one.
But, sometimes, you might find it convenient to use expressions like:
if(method()){
...
}
So, returning a boolean might be useful.
In the end, the two (throwing an exception and returning a boolean) don't exclude each other
Upvotes: 0
Reputation: 180917
When using exceptions for all error conditions, only return a value if the method has actual useful data to return.
If you have no useful information to return, use void.
Upvotes: 1
Reputation: 2036
I suggest returning void.
If you return a boolean, the calling code has to guess "Oh, should I check the boolean or check the Exception? Or maybe do both?"
Upvotes: 5