Only catch the exception if you can do something about it. Otherwise catch exceptions at the "highest" level in your application and do whatever is required to handle it including terminating your application.
In applications having a UI the "highest" level is often event handlers like the click handler for a search button. For background services the "highest" level is often thread procs or timer callbacks.
UI application Background service
| System Code | | System Code |
+----------------+ +----------------+
| Event Handler | <- try/catch here -> | Thread proc |
| | | |
| ... | | ... |
| | | |
| Method | <- throw here -> | Method |
If you let the exception propagate back into system code you will have an unhandled exception and you application will crash.
Handling an exception in a UI application often involves showing a message box. Some exceptions are not fatal and the operation may be retried (say if a file is missing or a database query failed) but other exceptions are fatal and the only option is to terminate the application.
A background service will log the exception and perhaps retry the operation. If several retries fail the logging level may increase to get the operators attention.
Good practice when it comes to exception handling:
- If you catch an exception and rethrow you own exception wrap the original exception as the
InnerException
of the new exception.
- If you catch an exception perhaps to do some cleanup but rethrow it because you want it to bubble up then always rethrow it using
throw
without specifying any exception. This will ensure that the original stack trace isn't destroyed.
- In most cases you should only ever catch the base class
Exception
in your top level handlers.
- Use
finally
blocks or even better the IDisposable
pattern to perform proper cleanup in your code.
- Think of exceptions as the "user interface" to the developer and format the exception messages accordingly. If you need a more polished user interface for the less technical user you should probably hide the more technical stuff.
- Try to only use exceptions for exceptional situations like unexpected errors. Don't throw exceptions for common error cases. Checking the existence of a key in a dictionary should not throw an exception but instead return true/false value.
To answer your specific question I don't think you should catch any exceptions in your property.