2

I have a .NET application (VB.NET) that was working fine until someone from Germany tried it.

I can see that the problem is im catching exceptions with "try catch exemption" and parsing the exemption string.

This works fine when the application is in English but is failing in any other language (obvious, but I never thought it would have such a global use).

So rather than rewritting all my error handling (where to start!) is there a way of forcing the .net application to use en-us?

Also what are the bad implications of doing this?

To expand further as this got marked as duplicate. I am starting a thread that begins with

Thread.CurrentThread.CurrentUICulture = New CultureInfo("en-us")

This works except for when it reaches this part of the thread

Dim client As WebClient = New WebClient()
Try
client.DownloadFile(url, tempName)
Catch ex As Exception
!!Ex is still giving the string in German rather than en-us
End Try

also, under Assembly Information it shows "English (United States)" as Neutral Language.

  • I have expanded the question. Language seems to revert to default for webclient.downloadfile exceptions. – JohnFrendras Mar 28 '16 at 21:34
  • Your question is either specific to C# _or_ VB.NET, or it is not specific to either. Please decide. You should have only one of those language tags at most. As for the question itself, I question the wisdom of parsing the exception text. That said, it looks like you are setting `CurrentUICulture`, which I would not expect to affect exceptions. Have you tried setting `CurrentCulture` instead? Please provide a good [mcve] that reliably reproduces your problem. – Peter Duniho Mar 29 '16 at 02:09
  • If the OP can manage C#, then he can have both tags. Stop that kind of unreasonable "banning" people rights, please, because much of the .Net programmers can manage and accept a solution for both Vb.Net or C#. – ElektroStudios Mar 29 '16 at 02:54
  • If the end-user has other .NET languages installed, then the question is instead this one "Exception messages in English?": http://stackoverflow.com/questions/209133/exception-messages-in-english which can't be solved easily, but there are some tricks... – Simon Mourier Mar 29 '16 at 05:21
  • I made a small example that shows why Im confused - but it wont let me edit the question now... Simply though:- 404 errors and similar get translated. Time out errors no not... CurrentCulture = New CultureInfo("en-us") - does nothing CurrentUICulture - partially translates as aboev – JohnFrendras Mar 29 '16 at 10:19

1 Answers1

1

Seems that you are trying to parse in some way the exception message, or you are trying to show it in English for the end-user, in both cases you could try this general solution to (try)convert the message exception to English culture (taken from here):

public module ExceptionExtensions

<DebuggerStepThrough>
<Extension>
<EditorBrowsable(EditorBrowsableState.Advanced)>
Public Function ToEnglish(Of T As System.Exception)(ByVal ex As T) As String

    Dim oldCI As CultureInfo = Thread.CurrentThread.CurrentUICulture
    Dim exEng As System.Exception = Nothing

    Task.Factory.StartNew(Sub()
                              Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US")
                              exEng = DirectCast(Activator.CreateInstance(ex.[GetType]), System.Exception)
                              Thread.CurrentThread.CurrentUICulture = oldCI
                          End Sub,
                          TaskCreationOptions.LongRunning).Wait()

    Return exEng.Message

End Function

end module

Usage Example:

 Try
     Throw New FileNotFoundException

 Catch ex As FileNotFoundException
     Dim message As String = ex.ToEnglish()

 End Try

Also, see my question and/or the comments below, they could be useful.

Community
  • 1
  • 1
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
  • While this returns the error in english, Im getting "Unable find the specified file" regardless if its a 404 or a time out. – JohnFrendras Mar 29 '16 at 11:39
  • In the example usage I used a `FileNotFoundException` ... you should just use the `Dim message As String = ex.ToEnglish()` in yout `Catch` block of your desired exception. – ElektroStudios Mar 29 '16 at 16:13