What is most important is not losing the stack trace, often the best plan is to let the exception bubble up to the outermost try / catch, to do that make sure you just throw
rather than throw ex
(in the code below). throw ex
is like throwing a new exception from that point in the code and may lose vital error info. The most important thing is that when someone comes to fix whatever problem occurred in the repository they have all the error details. It is usually better not to catch at all than to hide the real error.
You could catch in the repository and pass back a success / fail bool to the UI layer. If you do that be sure to log the whole stack trace and all inner execptions, consider calling the repository from a single point in your code where you are catching and logging all your exceptions, disparate logging with slightly different error handling all over the place can lead to lost error details.
Your try / catch in the repository could look like this:
try
{
// code which may cause an exception
}
catch (Exception ex)
{
// You can log here
throw;
}
This has been discussed a lot on various threads SO, you should read Throwing Exceptions best practices.