2

I have the following code:

if (smtpException != null)
{
    if (smtpException.InnerException.Message.Contains("net_io_connectionclosed"))
    {
        sendingFailedException = new EmailSendingFailedException(EmailsLocalization.EmailSendFailedWrongPortNumber, e);
    }

    if (smtpException.StatusCode == SmtpStatusCode.MustIssueStartTlsFirst || 
        smtpException.Message.Contains("AUTH"))
    {
        ...
    }
}

When I rely on exception message in my code can I be sure that it is in English?

3 Answers3

5

Generally: No, you can't.
For example, the language of the messages of exceptions thrown by the .NET framework depend on the language with which the .NET framework was installed. Messages from third party libraries can generally be in any language. And those libraries could also be localized, just as the .NET framework.

However, what you are checking for in your example doesn't look like human readable text but more like part of a protocol. Those keywords are derived from English but are now a language of their own. The language of that protocol. If you would derive equivalent keywords from another language, the protocol would no longer work, so you can be sure that the keywords stay the same, not matter the locale of the computer running your application or the server with which you are communicating.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • 1
    Strictly it's not even in English; it's in a very limited computer-processable language based on English. This perhaps pedantic point is relevant because while someone might change "near" to "proximate" or vice-versa, they won't change "AUTH" to "ALLOW". – Jon Hanna Feb 26 '16 at 15:53
  • @JonHanna: Absolutely correct. I adjusted my answer a bit. Do you think it conveys this better now? – Daniel Hilgarth Feb 26 '16 at 15:58
  • My message it's part of e.Message = "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at" or SMTPException : Unable to read data from the transport connection: net_io_connectionclosed –  Feb 26 '16 at 15:58
  • Yep, though I thought it a fine answer already, I think it even better now. – Jon Hanna Feb 26 '16 at 15:59
  • @ivan_petrushenko: As others have indicated: Checking the exception message for a certain string should be your last resort. Did you check if the exception is maybe of a specific type that provides you with more information via its properties? – Daniel Hilgarth Feb 26 '16 at 16:00
  • @DanielHilgarth Updated my question. –  Feb 26 '16 at 16:20
  • @ivan_petrushenko: As I said in my answer: You are not checking for English words in your code. You are checking for keywords that are derived from English words. Those keywords will always be the same no matter which language is being used. – Daniel Hilgarth Feb 26 '16 at 18:12
1

As far as I know the message is always in englis but you can rely on Hresult number instead of cheching the message text:

if(exception.HResult == somenumber){}

Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
0

You can process it like this:

catch (Exception e){  
    var w32ex = e as Win32Exception;
    if(w32ex == null) {
        w32ex = e.InnerException as Win32Exception;
    }    
    if(w32ex != null) {
        int code =  w32ex.ErrorCode;
        // do some processing
    }    
    // do the rest of your exception handling  
}

or use the HResult:

 catch (Exception ex){  
     int exCode = ex.HResult;
 }
Gnqz
  • 3,292
  • 3
  • 25
  • 35