user5515846
user5515846

Reputation:

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

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?

Upvotes: 2

Views: 160

Answers (3)

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174309

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.

Upvotes: 5

Gnqz
Gnqz

Reputation: 3382

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;
 }

Upvotes: 0

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34150

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){}

Upvotes: 1

Related Questions