8

Given an onion architecture, what are the advantages and disadvantages of throwing exceptions in the business logic (which is in the center of the onion) for invalid parameters provided by the user?

The alternative would be error codes.

I do have arguments for both approaches, exceptions vs. error codes, however I find it hard to decide.

Flavius
  • 257

2 Answers2

4

The business layer should not be getting values provided by the user, invalid or otherwise. The application layer should have already validated the values and converted them from the application domain to the business domain.

To put it another way, an exception thrown from the business layer should not bounce all the way back to the UI layer. It should be caught by the application layer and converted into a useful UI element.

Daniel T.
  • 3,053
2

There is a lot of material on SE and elsewhere dealing with Exceptions vs. Error Codes in general. There is nothing special about the business layer in this regard - try to handle errors as consistently as possible throughout the application.

So if you would use an Exception for validation errors in other parts of your application, do the same here.

doubleYou
  • 2,867
  • 1
  • 13
  • 26