试试Catch Block

时间:2011-09-07 12:54:36

标签: c# try-catch

我是C#的新手并且从未尝试过创建一个try catch块,我收到一个错误,其中错误表明"一个密钥已经存在"在HybridDictionary之一中,如何将.Adds放在Try catch块中并在密钥已存在时忽略它:

这是带有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;
        }
    }

8 个答案:

答案 0 :(得分:16)

作为 catch-all qaundary的替代方案,我建议通过Contains方法检查项目是否通过密钥存在。例如:

if (!rtfColor.Contains(RtfColor.White))
{
    rtfColor.Add(RtfColor.White, RtfColorDef.White);
}

让我们更进一步说明Jim B的建议(因为 每次添加会引入额外的行并且很快就会变得势不可挡),我们可以创建一个简单的方法来“安全地添加项目”,也就是说,只有当一个项目不存在特定键时才能将项目添加到集合中(您可以在方法命名和访问等方面应用更多特殊性,但作为示例):

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

如果需要,可以更简单地扩展这一点进行更新。

何时使用try / catch是另一种故事,何时使用try / catch来忽略错误是另一种生命。

答案 1 :(得分:2)

即使将代码包装到try catch块中,抛出异常的行之后的代码也不会执行,但只会执行catch / finally块中的代码,所以你应该包装所有{{1声明(这真的很糟糕) 你应该总是阻止你的代码抛出异常,并且在可能的情况下你应该避免它。如何创建一个方法(或扩展方法)来检查项目是否已经存在于数组中并且是否不是该项目将被添加? 看看下面的字典扩展方法,它适用于任何字典

.Add

答案 2 :(得分:1)

你可以在整个过程中添加一个try-catch块:

   try
    {
        // Your code inside here
    }
    catch (Exception e)
    {
        // do nothing and be silent
    }

但我必须说我不推荐这种方法。查看代码并寻找一种更好的更稳定的方法,而不是只是默默地吞下错误。

答案 3 :(得分:0)

你必须像这样包围每个添加:

try 
{
    rtfColor.Add(RtfColor.Aqua, RtfColorDef.Aqua);
}
catch (Exception e)  // use right type of exception here
{
    // log exception
}

但最好使用Disappointment先生的答案。

答案 4 :(得分:0)

忽略所有例外:

try {
    // Your code.
}
catch {
}

要忽略特定类型的例外:

try {
    // Your code.
}
catch (SpecificException) {
}

答案 5 :(得分:0)

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

答案 6 :(得分:0)

而不是try / catch,只需检查字典是否包含密钥:

if(!rtfColor.ContainsKey(RtfColor.Aqua))
{
   rtfColor.Add(RtfColor.Aqua, RtfColorDef.Aqua);
}

将整个事物包装在try / catch中的问题是,在抛出异常之后,您将错过尝试添加的任何值。

答案 7 :(得分:0)

你做不到。问题是,如果你得到一个异常,你将被抛出try try序列。您要么必须在所有对.Add方法的调用之前放置try / catch,要么尝试以更加优雅的方式执行,即:

  1. 形成所有RtfColor / RtfColorDef对的字典。
  2. 使用foreach循环迭代它。在foreach的主体中,检查您正在检查的对是否已经在rtfColor中。在这种情况下,只需要一次try / catch。确保你把catch {继续; }。