Just catch the exception if you can do something. Otherwise, catch exceptions at the "highest" level in your application and do whatever it takes to process it, including terminating your application.
In user interface applications, event handlers, such as the click handler for the search button, are often the “highest” level. For background services, the “highest” level is often streaming proc or timer callbacks.
UI application Background service | System Code | | System Code | +----------------+ +----------------+ | Event Handler | <- try/catch here -> | Thread proc | | | | | | ... | | ... | | | | | | Method | <- throw here -> | Method |
If you allow the exception to propagate back to the system code, you will have an unhandled exception and the application will crash.
Exception handling in a user interface application often involves displaying a message box. Some exceptions are not fatal, and the operation can be repeated (say, if the file is missing or the database query failed), but other exceptions are fatal, and the only option is to terminate the application.
The background service will log the exception and possibly retry the operation. If several attempts fail, the level of logging may increase to attract the attention of operators.
Good practice when it comes to handling exceptions:
- If you catch the exception and reinstall your own exception, wrap the original exception as an
InnerException new exception. - If you catch an exception, you might do some cleanup, but restore it because you want it to bubble, and then flip it over with
throw without specifying any exception. This ensures that the original stack trace is not destroyed. - In most cases, you should only sunbathe the base
Exception class in your top-level handlers. - Use
finally blocks or even better the IDisposable pattern to properly clean your code. - Think of exceptions as a “user interface” for the developer and format the exception messages accordingly. If you need a more polished user interface for a less technical user, you probably should hide more technical specifications.
- Try using exceptions only for exceptions, such as unexpected errors. Do not throw exceptions for common mistakes. Checking for a key in a dictionary should not throw an exception, but instead returns true / false.
To answer your specific question, I do not think that you should catch any exceptions in your property.
Martin liversage
source share