Alex Rivera | Logout

How to report standard exceptions to the user?

Asked 2012-07-01T16:04:16.907
10

Consider a C# GUI application which uses a FileStream to read a file, chosen by the user through an "Open File" dialog.

In case the read fails with one of the exceptions, what is the correct way to report the failure to the user, in an user-friendly manner?

Should I invent my own message for each of those exceptions, or is there a way of obtaining a localized, user-friendly message that I could present verbatim to the user?

Edit

I'm asking whether .NET itself is able to provide me with a descriptive string that I can present (and which would be consistent with other .NET programs). I know that I can roll up my own, but I'd like to avoid that if there's a standard alternative.

Edit
Report

1 Answer

1

If you are catching an exception thrown by one of the .Net framework's File classes, then it is likely that the contents of the exception's .Message property will already be localized. The .Message property is supposed to contain localized, human readable text. How 'friendly' it is depends, I guess, but it might contain something you can embed within a more general and friendly paragraph.

Assuming you might write some method AlertUserWithMessage() to display the error to the user, this might be useful:

try
{
    fileStream.Read(...);  // or some other operation
}
catch(Exception e)
{
    AlertUserWithMessage(e.Message);
}

If you want to include additional information that might be helpful to a support person diagnosing the problem, then you can also get the stack trace as a string from the exception.

try
{
    fileStream.Read(...);  // or some other operation
}
catch(Exception e)
{
    AlertUserWithMessageAndStackTrace(e.Message, e.StackTrace);
}
answered 2012-07-01T16:41:19.290

Your Answer