views:

32

answers:

3

If an error/exception occurs in a repository class, should that exception get

a. caught & logged, or
b. thrown to the caller (a service -- what should happen with it there?)

+1  A: 

If you can, handle the exception gracefully (catch, log, recover) in the repository and pass a failure condition back to your caller to allow the caller to handle the failure from their respect. If you can't then it's probably a fatal exception and time to log if you can and terminate.

Lazarus
A: 

The answer always with exceptions is "Deal with them where you can deal with them"

If, in the face of an exception, the repository can nevertheless formulate an acceptable return value, then the repositiory should handle it.

If the repository cannot, and only the callee has all the information needed to deal with the problem, then the exception should bubble up.

James Curran
A: 

In general I prefer to catch and log exceptions where they occur. So I will go with a. Usually at the point at which the exception occurs you have the most information on the error and can log a descriptive message, and it makes going through your log easier. If you hand off the error up the chain it makes finding where the error occurred that much harder. Not that it's that much harder to check the inner exception but depending on how far you pass it up, when it is logged you are no where near your original error.

Jack