Should I use custom exceptions to control the flow of application?

Posted by bonefisher on Stack Overflow See other posts from Stack Overflow or by bonefisher
Published on 2010-04-29T07:48:43Z Indexed on 2010/04/29 8:07 UTC
Read the original article Hit count: 285

Is it a good practise to use custom business exceptions (e.g. BusinessRuleViolationException) to control the flow of user-errors/user-incorrect-inputs???

The classic approach: I have a web service, where I have 2 methods, one is the 'checker' (UsernameAlreadyExists()) and the other one is 'creator' (CreateUsername())... So if I want to create a username, I have to do 2 roundtrips to webservice, 1.check, 2.if check is OK, create.

What about using UsernameAlreadyExistsException? So I call only the 2. web service method (CrateUsername()), which contains the check and if not successfull, it throws the UsernameAlreadyExistsException. So the end goal is to have only one round trip to web service and the checking can be contained also in other web service methods (so I avoid calling the UsernameAlreadyExists() all the times..). Furthermore I can use this kind of business error handling with other web service calls completely avoiding the checking prior the call.

© Stack Overflow or respective owner

Related posts about best-practices

Related posts about design