Delphiでは、次のようなことができます:
try
if not DoSomething then
Exit;
if not DoSomething2 then
Exit;
if not DoSomething3 then
Exit;
finally
DoSomethingElse;
end;
別の方法では、メソッドのDoSomething
結果が false の場合、プログラム フローは finally ブロックに転送され、実行さDoSomething2
れDoSomething3
ません。
C#でそのような動作を実現するにはどうすればよいですか?
前もって感謝します。
Edit1: 以下の例は VS 2008 ではコンパイルされません
Edit2:申し訳ありませんが、断食して return ステートメントを忘れてしまいました。
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
テスト後、これを達成する最も簡単な方法は、結果が false
の場合に例外をスローすることです。独自の例外をスローし、特定のメッセージを書き込んで、finally 句に渡すことができます。