comprehensive/descriptive catch statements needed

581
1
11-01-2010 07:54 AM
SimonWillis
New Contributor II
I would like to provide a more comprehensive and descriptive catch statement besides this:

                        catch (NullReferenceException e)
                        {
                            System.Diagnostics.Debug.WriteLine(e.Message);
                        }
                        catch (Exception e)
                        {
                            System.Diagnostics.Debug.WriteLine(e.Message);
                        }


Also, would it make sense to provide a messagebox.show or a system.diag.debug.writeline? What's the consensus here?
Thanks!
0 Kudos
1 Reply
AlexanderGray
Occasional Contributor III
if you use e.toString instead of e.Message, you get the call stack of the error and even the line number if you are running debug code.

Showing the user a message box with the error message is not very useful in my experience.  I usually log the message somewhere (file or event log) and pop up a message that an error happened and there is a message in a log.  The user can then send the log to support.  I typically write the message to the trace and have a trace listener write it to a log.  That allows you to change how you log things without code changes.  Also when you run in debug mode in VS, the trace gets written to the output window so there is no need for message boxes.  However, message boxes are useful in debug mode because they are annoying.  A developer sufficiently annoyed by a message box will fix it rather than just ignore it.
0 Kudos