I doubt it would be helpful to have a common place for reporting errors.
What do you want to achieve - code reduction?
Image is an InvalidFileFormatException exception that may occur in your application when you try to open a file whose format is not as expected. A global exception handler might register this. Then your log file will read something like:
[Yesterday...] The file format is invalid: InvalidFileFormatException. StackTrace: ...
But what do you get from this information? Well, I admit that if there is only one call in your application that could throw this exception, everything will be okay. But what if there are multiple calls to the same method or if other called methods throw the same exception?
You should rely on a detailed exception message, but unfortunately, when it comes to exceptions thrown in Runtime, you cannot influence the messages. It would not be better to have something like
string fileName = @"C:\Users\stackoverflow\Documents\file.frk"; try { FreakingObject fo = freakingObjectConverter.ReadFromFile(fileName, FreakFormat.AutoDetect); } catch (InvalidFileFormatException iffe) { MyLogger.LogError("File " + fileName + " had an invalid format:", iffe); }
In this example, you at least get information about the malformatted file. You can easily create more complex examples (HttpRequest, etc.), where you can add very useful information to your journal, if only you knew about the context in which the Exception was added.
A small hint of try-catch around Application.Run(...) : keep in mind that whenever you reach the catch block, your application terminates if you do not recreate the main form or do nothing.
source share