I would suggest that if you do not know what to do with an exception, do not understand it. Too often, coders catch exceptions and simply swallow them whole.
catch (Exception ex) { }
Clearly, this is not good, because bad things are happening and no action is being taken. Exclude only exceptions that may be valid!
This suggests that graceful error handling is important. You don’t want your application to crash, do you? You may find ELMAH useful for web applications, and the global exception handler is pretty easy to configure for WinForms or XAML desktop applications.
Combining all this, you can find this strategy useful: 1. catch certain exceptions that, as you know, may occur (DivideByZeroException, SQLException, etc.) and avoid the general Exception; 2. Raise the exception again after handling it. eg.
catch (SQLException ex) { throw;
What can you really do with SQLException? Did you connect to the database? Was this a bad request? You probably don't want to add all the processing logic for this, and besides, what if this is something you did not expect? Therefore, simply handle the exception, if you can, raise it if you are not sure that it is allowed and gracefully handle it at the level (for example, show a message like "Something went wrong, but you could continue working. Details:" [ex.Message] ". Abort or Retry?")
NTN!
John Saunders, thanks for the fix.
Todd Sprang Apr 08 2018-10-12T00: 00Z
source share