Say I have two classes : a provider A and a consumer B.
My question : who should do the error handling, validation etc.?
Example : a Garage class and a Car class.
Should the Garage class tell the Car class : "He Car you are to big to enter my garage door." Or should the Car class say: "This garage door is to small for me to enter."

8 Years
Discussion Span
Last Post by ddanbe

Define 'provider' and define 'consumer'. What do you mean by 'putting a car in a garage'? That analogy can mean different things. (Maybe just tell us what you're doing.)


I tought I was rather clear but then again not.
To explain something to a person in writing is as difficult as explaining something to a computer in talking...

A provider class as I see it is a class that , well, provides some services that a consumer class can "consume".
I tought the metaphore of a car and garage would do the trick to make it as simple as possible to explain.
The real world example is about a basic matrix manipulating class and a user interface class. Which one of the two has to do the error checking(eg you cannot add 2 matrices with different rows and columns)Should I check that in the user interface or should I let the matrix class handle that?


Well clearly, the matrix manipulation class should provide clear error messages, throwing exceptions when you try to do impossible things. What do you expect integers to do when you divide by zero? Would you rather report an array out of bounds exception or have a clearer exception that explains that the matrices are of the wrong dimensions?

So the matrix class should throw the exception, and either the user interface should avoid adding mismatched matrices in the first place, or, if you like, catch the matrix class's exception and report the error. You'd probably want a special subclass of InvalidOperationException that users of the matrix can catch to handle errors with their matrices, rather than an internal error in the program.

Regarding cars and garages -- well, who has what responsibility depends on how you're representing the physical universe.


Thank you Rashakil for the clear explanation. I thought I could make the matrix class simpler by not doing any error handling in there. You got a strong point : it is unavoidable that during some calculations a division by zero might pop up, so error checking is needed. So I better do it well and concise in the matrix class and do minor checking and handling in the class that uses the matrix class.
Thanks again.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.