我是写作课。下面是功能之一:C# - 抛出异常类
public string GetAttribute(string attrName)
{
try
{
return _config.AppSettings.Settings[attrName].Value;
} catch(Exception e)
{
throw new ArgumentException("Element not exists", attrName);
return null;
}
}
然后,我使用它的主要形式MessageBox.Show(manager.GetAttribute("not_existing_element"));
Visual Studio中抛出的线路异常:throw new ArgumentException("Element not exists", attrName);
,但是,我希望得到一个异常在线MessageBox.Show(manager.GetAttribute("not_existing_element"));
我该怎么做? 上传:对不起,英语不好。
不知道我完全同意这一点 - 如果设置不存在,将返回一个空值。这不会导致MessageBox.Show调用的问题吗? – Tim
@Tim - 异常会冒泡。 'MessageBox.Show'不会被执行。如果'attrName'不存在,'_config.AppSettings.Settings [attrName]'将是'null',对'.Value'的调用将导致'NullRefereceException'。 – Oded
好的,现在我明白了(在我睡觉的时候)。谢谢:) – Tim