![]() New ApplicationException("Unable to calculate !", ex).Log().Display() Add some user-friendly message to an exception They are implemented in a way they can be chained together, and it is very easy to add your own caught exception processing. Here is an example of extensions methods to handle caught exceptions a comfortable way. There is a lot to do with exception handling (Custom Exceptions) but those rules that I try to keep in mind are enough for the simple applications I do. Throw new ApplicationException(string.Format("I cannot write the file ", fileName, directoryName), ex) Throw new ApplicationException("Something wrong happened in the calculation module:", ex) Add useful information to the exception What I do in some called functions: // Calculation module What I do at the top level: // i.e When the user clicks on a buttonĮx.Log().Display() // Log exception, then show it to the user with apologies. I WANT THIS CODE TO RUN EVENTUALLY REGARDLESS AN EXCEPTION OCCURRED OR NOT If an exception occurs in the following code, then the finally will be executed Having a try finally without a catch is perfectly valid: try Reusable or deep called functions does not need to display or log exceptions : they are either bubbled up automatically or rethrown with some custom messages in my exception handlers.It is not necessary to put exception handlers everywhere. Remember ALL exceptions are bubbled up to the top level.It is a good practice to design some static methods to handle exceptions in the application top level error handlers. The user does not care, but it is important to know what happened. EndUpdate in the finally section during a TreeView fill) Require some extra processing to put things together when they happen to avoid cascading problems (ie: put. In such a case, I throw a new ApplicationException("custom message", innerException) to keep track of what really happenedĪdditionally, I try my best to sort exceptions correctly. All the operations that I know might not work all the time (IO operations, calculations with a potential zero division.).All events fired by third party components. ![]() All events fired by the WinForms infrastructure (Load, Click, SelectedChanged.).Then I always enclose every piece of code that is run externally in try/catch : For a Service or a Console application: log it to a file (service or console).For a UI application: to pop it to the user with an apology message (WinForms).To catch all unhandled exceptions by hooking to the Application.ThreadException event, then decide:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |