locked
Using resources with Exceptions RRS feed

  • Question

  • User1963151947 posted

    I am looking for the best way to handle exception messages in a multi-language site.  I understand how resources work and I can get the message of the exception from the resource file without a problem.  The problem I am having is the way the language is choosen for the exception message.  From my understanding, exceptions in general are not for the general user to see.  Not even the exception message.  No user out there should get and error saying "Object reference not set to an instance of an object." or a foreign key constraint error from the database.  To me, those exception messages are for us as developers to use to determine what the problem is.  If it has to do with the information entered, we add code to check that and if it is invalid, display a meaning message to the user so the exception does not even occur. Or we catch the exception and write code to display the information to the user correctly. Now where my problem comes in is when dealing with multiple languages. I have a site that may have English, German, French, etc... as available languages on my site.  So I need to set the CurrentUICulture to the correct Culture so the resources for the correct language is used.  But I don't want my exceptions to be in French or German.  I want them to be in English.  So I don't want the CurrentUICulture to be used, which is what happens in all of the built in exceptions in the framework.  This application is designed to be sold and installed along with an API so other developers can use the application, so I do need to exception messages to support different languages, just not necessarily what the ui culture is.

     So basically what I am asking is if there is currently a way to set the culture for the exceptions to be different the the current UI culture?

    Friday, November 3, 2006 8:20 AM

Answers

  • User-503940700 posted

    So basically what I am asking is if there is currently a way to set the culture for the exceptions to be different the the current UI culture?

    As far as I know, the answer to the question above is "no". But why would you want the exceptions to be in English? Suppose someone has German locale set and using your app, he should ideally get exceptions in German only.

    -Vivek

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, November 4, 2006 4:13 AM

All replies

  • User-503940700 posted

    Hello,

    Though I am not very clear with the way you explained your scenario It seems you want your API users to see the thrown exceptions in English language, irrespective of the culture, then I think you need to focus on thrown exceptions by the lower layers as all exceptions which are caught by the GUI will be handled and a user friendly message in the custom language will be shown to them. For exceptions simply thrown, you should not set the culture at all (I am assuming these classes would be in a separate layer, like BL class library).

    All exceptions handled by the GUI will use resource files, but API users should get exceptions in default culture only.

    HTH,

    Vivek

    Friday, November 3, 2006 1:34 PM
  • User1963151947 posted
    That is not true.  Any exception, no matter where it is thrown or caught, uses the CurrentUICulture of the thread. If you use reflector and look at the implementation of the constructor of ArgumentNullException you will see what I mean.  It pulls the message from the resource file using the CurrentUICulture from the thread.
    Friday, November 3, 2006 1:47 PM
  • User-503940700 posted

    That is not true.  Any exception, no matter where it is thrown or caught, uses the CurrentUICulture of the thread. If you use reflector and look at the implementation of the constructor of ArgumentNullException you will see what I mean.  It pulls the message from the resource file using the CurrentUICulture from the thread.

    That's true but I do not know how your app is structured. I thought that the culture settings are all in the GUI and if the users are using your library dll's they are not concerned with the GUI at all. So they should be receiving the errors in the default culture as the current thread's culture is not set by your GUI. It's upto them to choose their culture.

    Let me know your thoughts here.

    Vivek

    Friday, November 3, 2006 1:59 PM
  • User1963151947 posted
    The idea is that using the API other developers can create plug-ins and modules which can be added to the application. So the code they write using the API would be used in the GUI which would report an unhandled exception back.  Does that answer your question?
    Friday, November 3, 2006 4:47 PM
  • User-503940700 posted

    So basically what I am asking is if there is currently a way to set the culture for the exceptions to be different the the current UI culture?

    As far as I know, the answer to the question above is "no". But why would you want the exceptions to be in English? Suppose someone has German locale set and using your app, he should ideally get exceptions in German only.

    -Vivek

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, November 4, 2006 4:13 AM
  • User1963151947 posted
    The point I have been trying to make is that I don't think they user should get the exception message.  Most of the time those exception messages mean nothing to them, they are messages indicating a problem with the code.  For example, an object reference exception or a foreign key constraint error is not something a user would understand or should even see.  Any exception that occurs and is presented to the user could open up security issues.  Code should be written to handle the exceptions correctly and displayed to the user.  Any exception that has not or can not be programmed for should show the user a general error message.  Now the most important part of this is that all exceptions that are not handled getting forwarded back to the development team so that issue can be addressed.  Whether it is having it display a simple error message or writing code to prevent that exception from occurring depends on what the exception is. Now if the user has a German locale set and an exception occurrs I would want a message going out to the user in German, but I would want the exception to be sent to the development team in English.  I can't read German, and a German exception message explaining the problem will not help. So in code, I can catch the exception and use a resource file to retrieve a German message to display on a web page, but the exception needs to be in the langauge the development team can understand so they can address the issue.
    Monday, November 6, 2006 8:23 AM