Reputation: 41
I am new to C# and never tried to create a try catch block, I am getting an error where the error indicates "A key already exists" in one of the HybridDictionary, How can I put the .Adds in a Try catch block and ignore it if the key already exists:
here is the original code with the 2 HybridDictionaries:
public MWRichTextBox() : base() {
// Initialize default text and background colors
textColor = RtfColor.Black;
highlightColor = RtfColor.White;
// Initialize the dictionary mapping color codes to definitions
rtfColor = new HybridDictionary();
rtfColor.Add(RtfColor.Aqua, RtfColorDef.Aqua);
rtfColor.Add(RtfColor.Black, RtfColorDef.Black);
rtfColor.Add(RtfColor.Blue, RtfColorDef.Blue);
rtfColor.Add(RtfColor.Fuchsia, RtfColorDef.Fuchsia);
rtfColor.Add(RtfColor.Gray, RtfColorDef.Gray);
rtfColor.Add(RtfColor.Green, RtfColorDef.Green);
rtfColor.Add(RtfColor.Lime, RtfColorDef.Lime);
rtfColor.Add(RtfColor.Maroon, RtfColorDef.Maroon);
rtfColor.Add(RtfColor.Navy, RtfColorDef.Navy);
rtfColor.Add(RtfColor.Olive, RtfColorDef.Olive);
rtfColor.Add(RtfColor.Purple, RtfColorDef.Purple);
rtfColor.Add(RtfColor.Red, RtfColorDef.Red);
rtfColor.Add(RtfColor.Silver, RtfColorDef.Silver);
rtfColor.Add(RtfColor.Teal, RtfColorDef.Teal);
rtfColor.Add(RtfColor.White, RtfColorDef.White);
rtfColor.Add(RtfColor.Yellow, RtfColorDef.Yellow);
rtfColor.Add(RtfColor.WhiteSmoke, RtfColorDef.WhiteSmoke);
// Initialize the dictionary mapping default Framework font families to
// RTF font families
rtfFontFamily = new HybridDictionary();
rtfFontFamily.Add(FontFamily.GenericMonospace.Name, RtfFontFamilyDef.Modern);
rtfFontFamily.Add(FontFamily.GenericSansSerif, RtfFontFamilyDef.Swiss);
rtfFontFamily.Add(FontFamily.GenericSerif, RtfFontFamilyDef.Roman);
rtfFontFamily.Add(FF_UNKNOWN, RtfFontFamilyDef.Unknown);
// Get the horizontal and vertical resolutions at which the object is
// being displayed
using(Graphics _graphics = this.CreateGraphics()) {
xDpi = _graphics.DpiX;
yDpi = _graphics.DpiY;
}
}
Upvotes: 1
Views: 666
Reputation: 45068
As an alternative to a catch-all qaundary, I'd suggest checking if the item exists via key by means of the Contains
method. For instance:
if (!rtfColor.Contains(RtfColor.White))
{
rtfColor.Add(RtfColor.White, RtfColorDef.White);
}
Let's take this a little further to exemplify Jim B's suggestion (since this does introduce extra lines per add and could quickly become overwhelming), we can create a simple method to "add items safely", that is, add items to the collection only if an item doesn't already exist with a specific key (you may apply more specificity in terms of method naming and access etc, but as an example):
private void AddItemToDictionary(HybridDictionary dictionary, object key, object value)
{
if (!dictionary.Contains(key))
{
dictionary.Add(key, value);
}
}
AddItemToDictionary(rtfColor, RtfColor.Black, RtfColorDef.Black);
AddItemToDictionary(rtfColor, RtfColor.White, RtfColorDef.White);
AddItemToDictionary(rtfColor, RtfColor.Red, RtfColorDef.Red);
This could be extended rather simply to do the updating, if necessary.
When to use try/catch is kind of another story, when to use try/catch to ignore errors is another lifetime.
Upvotes: 16
Reputation: 26737
even if you wrap your code into a try catch block the code after the line that throws the exception won't be execute but only the code within the catch/finally block will be executed so you should wrap all the .Add
statement (which would be really really bad)
You should always prevent your code from throwing exception and when is possible you should avoid it. What about creating a method (or an extension method) that check if the item already exist into the array and if it is not the item will be added?
have a look at the below dictionary extension method it will work for any dictionary
public static class DictionaryExtension
{
public static void AddItemIfNotExist<TKey,TValue>(this Dictionary<TKey,TValue> dictionary, TKey key,TValue item)
{
if (!dictionary.ContainsKey(key))
{
dictionary.Add(key, item);
}
}
}
Upvotes: 2
Reputation: 10509
You can't. The problem is that if you get an exception, you will get thrown out of the try sequence. You either will have to put try/catch in front of all the calls to an .Add method, or try to do it in a more elgant way, which is:
Upvotes: 0
Reputation: 4032
You could just add a try-catch block around the whole thing like so:
try
{
// Your code inside here
}
catch (Exception e)
{
// do nothing and be silent
}
But I must say that I do not recommend this approach. Look through your code and look for a better more stable approach than just silently swallowing an error.
Upvotes: 1
Reputation: 7105
Instead of try/catch, just check if the dictionary contains the key already:
if(!rtfColor.ContainsKey(RtfColor.Aqua))
{
rtfColor.Add(RtfColor.Aqua, RtfColorDef.Aqua);
}
The problem with wrapping the whole thing in a try/catch is that you will miss any values you attempted to add after the one throwing the exception.
Upvotes: 0
Reputation: 31251
public MWRichTextBox() : base() {
// Initialize default text and background colors
textColor = RtfColor.Black;
highlightColor = RtfColor.White;
try
{
// Initialize the dictionary mapping color codes to definitions
rtfColor = new HybridDictionary();
rtfColor.Add(RtfColor.Aqua, RtfColorDef.Aqua);
rtfColor.Add(RtfColor.Black, RtfColorDef.Black);
rtfColor.Add(RtfColor.Blue, RtfColorDef.Blue);
rtfColor.Add(RtfColor.Fuchsia, RtfColorDef.Fuchsia);
rtfColor.Add(RtfColor.Gray, RtfColorDef.Gray);
rtfColor.Add(RtfColor.Green, RtfColorDef.Green);
rtfColor.Add(RtfColor.Lime, RtfColorDef.Lime);
rtfColor.Add(RtfColor.Maroon, RtfColorDef.Maroon);
rtfColor.Add(RtfColor.Navy, RtfColorDef.Navy);
rtfColor.Add(RtfColor.Olive, RtfColorDef.Olive);
rtfColor.Add(RtfColor.Purple, RtfColorDef.Purple);
rtfColor.Add(RtfColor.Red, RtfColorDef.Red);
rtfColor.Add(RtfColor.Silver, RtfColorDef.Silver);
rtfColor.Add(RtfColor.Teal, RtfColorDef.Teal);
rtfColor.Add(RtfColor.White, RtfColorDef.White);
rtfColor.Add(RtfColor.Yellow, RtfColorDef.Yellow);
rtfColor.Add(RtfColor.WhiteSmoke, RtfColorDef.WhiteSmoke);
// Initialize the dictionary mapping default Framework font families to
// RTF font families
rtfFontFamily = new HybridDictionary();
rtfFontFamily.Add(FontFamily.GenericMonospace.Name, RtfFontFamilyDef.Modern);
rtfFontFamily.Add(FontFamily.GenericSansSerif, RtfFontFamilyDef.Swiss);
rtfFontFamily.Add(FontFamily.GenericSerif, RtfFontFamilyDef.Roman);
rtfFontFamily.Add(FF_UNKNOWN, RtfFontFamilyDef.Unknown);
}
catch
{
}
// Get the horizontal and vertical resolutions at which the object is
// being displayed
using(Graphics _graphics = this.CreateGraphics()) {
xDpi = _graphics.DpiX;
yDpi = _graphics.DpiY;
}
}
Upvotes: 0
Reputation: 52137
To ignore all exceptions:
try {
// Your code.
}
catch {
}
To ignore just an exception of particular type:
try {
// Your code.
}
catch (SpecificException) {
}
Upvotes: 0
Reputation: 35793
You'd have to surround each add like this:
try
{
rtfColor.Add(RtfColor.Aqua, RtfColorDef.Aqua);
}
catch (Exception e) // use right type of exception here
{
// log exception
}
But it would be better to use Mr Disappointment's answer.
Upvotes: 0