在德尔福我可以做这样的事情:
try
if not DoSomething then
Exit;
if not DoSomething2 then
Exit;
if not DoSomething3 then
Exit;
finally
DoSomethingElse;
end;
换句话说,如果方法DoSomething
结果为假,则程序流被转移到 finally 块并且DoSomething2
不DoSomething3
被执行。
如何在 C# 中实现这种行为?
提前致谢。
Edit1: 下面的示例在 VS 2008 中无法编译
Edit2:对不起,我太快忘记了退货声明;
XElement OrderStatus(q_order_status Request)
{
XElement Response;
try
{
if (DoSomething() != 0 )
{
return;
}
}
catch(Exception e)
{
// catch some errors and eventually pass the e.Message to the Response
}
finally
{
Response = new XElement("SomeTag", "SomeResponse");
}
return Response;
}
Edit3:
经过测试,似乎实现这一目标的最简单方法是如果结果DoSomething1
为假则抛出异常。我可以抛出我自己的执行,写一个特定的消息并将它传递给 finally 子句。