fea*_*net 1 .net c# exception-handling parameter-passing
CreateDocument(string templatePath)
{
Document doc = OpenDocument(templatePath);
Picture pic = GetLogo();
AddLogo(doc, pic, templatePath);
}
AddLogo(Document doc, Picture logo, string templatePath)
{
Picture placeholder = doc.FindLogoPlaceholder();
if (placeholder.Size != logo.Size)
{
throw new ApplicationException(
String.Format("Invalid template {0}, logo size: {1}, required: {2}",
templatePath, placeholder.Size, logo.Size
));
}
}
Run Code Online (Sandbox Code Playgroud)
考虑上面的代码作为我刚刚编写的一个例子.
请注意,templatePath传递给AddLogo方法的唯一原因是为了便于异常生成.
我今天的代码中有一些东西需要我这样做,感觉就像一个非常讨厌的代码味道给我.但我不太熟悉异常处理模式,我真的没有看到更好的方法.
我想知道你的想法是什么,以及是否有更好的模式来处理这样的情况.
在更高级别创建例外:
CreateDocument(string templatePath)
{
Document doc = OpenDocument(templatePath);
Picture pic = GetLogo();
try {
AddLogo(doc, pic);
} catch (InvalidLogoSize e) {
throw new ApplicationException(
String.Format("Invalid template {0}, logo size: {1}, required: {2}",
templatePath, e.pSize, e.lSize
));
}
}
AddLogo(Document doc, Picture logo)
{
Picture placeholder = doc.FindLogoPlaceholder();
if (placeholder.Size != logo.Size)
{
throw new InvalidLogoSizeException(placeholder.Size, logo.Size);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
84 次 |
| 最近记录: |