C# – Localization of ArgumentException.Message in .NET

c++localization

In my localizable app I throw an ArgumentException like this:

throw ArgumentException("LocalizedParamName", "LocalizedErrorMessage");

And I catch it like this:

catch (Exception ex)
{
    Display(ex.Message);
}

I'm getting the resulting error message as:

LocalizedErrorMessage
Parameter name: LocalizedParamName

The problem here is "Parameter name: ", which is in english rather than my application's language. I suppoose that string is in the language of the .NET framework. Can anyone confirm that?

One workaround is doing this:

catch (ArgumentException ex)
{
    Display((ex as Exception).Message  + "\n" + "Translated(Parameter name:)"+ ex.ParamName);
}
catch (Exception ex)
{
    Display(ex.Message);
}

Is there any other more elegant way?

Best Solution

The message in exceptions should be for developers, not for end users. You should always attempt to catch your exceptions in such a way that you can display a meaningful error message. (No, "The program has encountered an unexpected error and will now exit" is not meaningful.) The reason why you shouldn't display the message of an exception to an end user is because the user doesn't care that the developer passed the wrong argument from function Foo() to function Bar(), let alone how it was wrong. But when you get around to fixing the bug, you'll need to know that.

So, don't worry about internationalizing your exception messages but do internationalize your messages to the user telling them what happened, what you're doing to fix it and hopefully how they can avoid the problem in the future.