4

I am using a try ... catch block in main.

When i set the following

Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");

This takes care of all the exceptions and prints them in English

But this fails for many API's internally especially for Exceptions caught like directory access ones. Is there something else that needs to be done?

So the main problem is this Execption

System.Net.Sockets.SocketException (0x80004005):

The message after this does not appear in the locale set

Nida Sahar
  • 698
  • 4
  • 13
  • 29
  • I don't know for sure, but think the locale of the exceptions are also bound to the installed .NET framework and system language. – dowhilefor Oct 04 '12 at 09:15
  • Please don't add tags in the question title. – Mario S Oct 04 '12 at 09:20
  • That's the worst question mark I've seen in a while. If it really means "But this fails?" then just try it. If it really means "But this fails!" then keep in mind that the culture must be in effect when the exception is *thrown*, not when it is caught. – Hans Passant Oct 04 '12 at 09:22

3 Answers3

1

I was able to do this by using Win32 FormatMessage Code block

[DllImport("Kernel32.dll", SetLastError=true)]
static extern uint FormatMessage( uint dwFlags, IntPtr lpSource, 
   uint dwMessageId, uint dwLanguageId, ref IntPtr lpBuffer, 
   uint nSize, string[] Arguments);

and by setting langid as english!

Backs
  • 24,430
  • 5
  • 58
  • 85
Nida Sahar
  • 698
  • 4
  • 13
  • 29
0

This issue can be partially worked around. The Framework exception code loads the error messages from its resources, based on the current thread locale. In the case of some exceptions, this happens at the time the Message property is accessed.

For those exceptions, you can obtain the full US English version of the message by briefly switching the thread locale to en-US while logging it (saving the original user locale beforehand and restoring it immediately afterwards).

Doing this on a separate thread is even better: this ensures there won't be any side effects.

Reference: Exception messages in English?

Community
  • 1
  • 1
Furqan Safdar
  • 16,260
  • 13
  • 59
  • 93
0

Don't choose culture for user. Let him do it himself.
There are rare cases, when you have to use predefined culture, but in most cases it's the user's business - what culture to use.

Dennis
  • 37,026
  • 10
  • 82
  • 150
  • 5
    I couldn't disagree more strongly. This isn't a case of selecting the culture for the user, as exception texts are not for the benefit of the user. Lots of programs catch all exceptions and pop-up a bug report dialog that lets you send the report to the developer. What the heck am I supposed to do with an exception message in freaking Russian or Korean? They're useless like that and the user doesn't need to see them. – KatDevsGames Oct 06 '13 at 13:09