简体   繁体   中英

How to Throw Multilingual Exceptions

I have a multilingual web application, where I use a resource file in the frontend to display the different text.

I followed this approach http://www.codeproject.com/Tips/586948/ASP-NET-Website-and-Csharp-with-Multi-Language

Is there a way where the exception handling done in the backend throws exceptions according to the current language selected?

You need CustomException which inherit Exception for this.(Be aware never catch all the exceptions only the exception needed for your case, I just wrote you an example. If you catch all the exception you can lose specific information when something go wrong on productive server. Like I said in the past I used it for DBConcurrencyException)

Business Object logic

try
{
    //some code
}
catch(Exception ex)
{
   CustomException newEx = new CustomException();
   newEx.Message = "Translate Multilanguage String depending of the user culture"
   throw newEx;
}

UI-part aspx.Page

try
{
    //some code which calls the BO logic with try/catch
}
catch(CustomException ex)
{
    Label1.Text = ex.Message;
}

How to use this for validation purpose:

if(myValue < 5)
   throw new CustomException("Value should not be under 5"); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM