It really depends on the function, but in general I would advice against having it display a MessageBox to the user; this is a task for the UI. If the function (for any valid reason) does show a MessageBox, provide an overload that is guaranteed to be "silent", so that a caller of the function can prevent the MessageBox. But, as I said, I think it's better to let the caller decide how to handle the exception. After all, it may be that the function is invoked from a Windows Service, where there is no UI.
I usually use one of these approaches:
- Not catch the exception at all, but rather let it bubble upwards. This is done by simply not implementing a try-catch block in the function.
- Catch the exception, do something with it (such as logging it), and then re-throw it.
- Catch the exception, wrap it (as an InnerException) in a new exception that is thrown, possibly with some logging done.
This applies if the function itself cannot handle the exception. In some cases it may be that the function can handle the exceptional state and still perform its work. In these cases of course no exception will be thrown by the function.