Localization suggestions for exceptions
I need to add localization to the exceptions thrown by my application as many are ApplicationExceptions and are handled and logged in the bug report. Ideally, I want to throw a new exception that inherits from ApplicationException that I can pass the resource key as well as the arguments, so that a mess can be created from the resource information. Unfortunately (I think) the only way to set the message in an exception is in New () ...
I would like something like:
public class LocalizedException
Inherits ApplicationException
public Sub New(ResourceKey as string, arg0 as Object)
MyBase.New()
' get the localized text'
Dim ResMan as New Global.System.Resources.ResourceManager("AppName.ExceptionResources", _
System.Reflection.Assembly.GetExecutingAssembly)
Dim LocalText as string = ResMan.GetString(ResourceKey)
Dim ErrorText as String = ""
Try
Dim ErrorText = String.Format(LocalText, arg0)
Catch
ErrorText = LocalText + arg0.ToString() ' in case String.Format fails'
End Try
' cannot now set the exception message!'
End Sub
End Class
However, I can only have MyBase.New () as the first line ReadOnly message
Does anyone have any advice on how to get the localized strings into an Exception handler? I will need this for several different exceptions, although I could go the route of an exception function that gets the localized string and throws the exception, although then the stack information is wrong. I also don't want too much in the main corpus before throwing, as it is obviously starting to drop into stream readability.
source to share
Here is an example of what I am doing. EmillException inherits from ApplicationException.
namespace eMill.Model.Exceptions
{
public sealed class AccountNotFoundException : EmillException
{
private readonly string _accountName;
public AccountNotFoundException(string accountName)
{
_accountName = accountName;
}
public override string Message
{
get { return string.Format(Resource.GetString("ErrAccountNotFoundFmt"), _accountName); }
}
}
}
source to share
take a look at this:
http://visualstudiomagazine.com/features/article.aspx?editorialsid=2562
I don't need to deal with localization, but it makes sense.
source to share