0

I know its never a good idea to use exception as business logic in your java code. When is it OK to use exception handling for business logic?. However I have seen at my job that people are still doing this, I have raised my concerns and I've been told I was wrong / crazy.

I consistently see people throwing runtime exceptions to later have the advice controller catch them and respond to the client. To the people using this technique it is a simple solution to have a return type that is different then what their controller returns.

For more clarification: Lets say the app is calling out to another service over http and you get a 404. The application will throw an exception so that the advise controller will respond with a custom response.

Community
  • 1
  • 1
  • 1
    Your example sounds more like a custom error page or application specific exception rather than "business logic". Getting a 404 from a web service call would usually be considered an exceptional condition (unless you're writing web service monitoring software). – Nate Jan 12 '16 at 02:27
  • My example is bad. They were also throwing exceptions if the input they were expecting was malformed. Same as above it was also being used to make the advise controller respond. I just thought of why this would be terrible.. This would be making a DDoS attack 100x more effective. – Tom Breitkopf Jan 12 '16 at 02:51
  • Exceptions for validation logic is not a good idea. See http://martinfowler.com/articles/replaceThrowWithNotification.html – DominikM Jan 12 '16 at 09:23
  • Worked on many legacy application that designed like that, exception flow behavior. IMO it's bad design, it's much simpler to just use an enum as response and deal with it. Exceptions are costly. – Davi Alves Jan 12 '16 at 13:08

0 Answers0