在我的对象转换的代码,我有一大堆:如何在处理C#中的异常时压缩大量小的Try-Catch块?
try
{
NativeObject.Property1= int.Parse(TextObject.Property1);
}
catch (Exception e)
{
Trace.WriteLineIf(ConverterSwitch.TraceVerbose, e);
}
try
{
NativeObject.Property2= DateTime.Parse(TextObject.Property2);
}
catch (Exception e)
{
Trace.WriteLineIf(ConverterSwitch.TraceVerbose, e);
}
等等......我不希望所有转换失败的某些属性的原因,所以我不能把这一切都在一个try块,但我需要登录,如果有什么失败,并继续..
有没有办法来压缩所有这些尝试抓住的东西?
可惜我们不能像C#代码写:
try
{
int num = int.Parse("3");
decimal num2 = decimal.Parse("3.4");
}
catch (Exception e)
{
Trace.Write(e);
continue; //continue execution from the point we left. (line 2)
}
如果切换到VB.NET,你可以使用'上的错误恢复Next` :-) – 2010-12-09 16:27:45
某处VB6/VBA开发者在你的窃笑。从来没有想过我会听到有人松“因为错误恢复下一个” – JohnFx 2010-12-09 16:28:24
@Cody:哦,上帝。哦,上帝不。请不。 – cdhowie 2010-12-09 16:29:15