次のような状況の場合:
- Execute()は新しいスレッドを作成し、その中で関数GetSession()を実行します。
- Execute()は、独自のスレッドでGetSession()を再度実行します。
- Execute()は、(1)のスレッドに参加します。
私の質問は:
Execute()が実行されているスレッドが現在GetSession()自体を実行しているときに、GetSession()が(1)で生成されたスレッドから例外をスローした場合はどうなりますか?
余分なスレッドから再スローされた例外は、Execute()まで伝播し、別のスレッドからのものであっても、ハンドラーに移動しますか?
この問題を示すためのサンプルコードを次に示します。
ここのウィンドウでこれを作成しました(これはモックアップです)ので、構文エラーについてお詫びします。
public void Execute()
{
//Some logon data for two servers.
string server1 = "x", server2 = "y", logon = "logon", password = "password";
//Varialbes to store sessions in.
MySession session1, session2;
try
{
//Start first session request in new thread.
Thread thread = new Thread(() =>
session1 = GetSession(server1, logon, password));
thread.Start();
//Start second request in current thread, wait for first to complete.
session2 = GetSession(server2, logon, password));
thread.Join();
}
catch(Exception ex)
{
//Will this get hit if thread1 throws an exception?
MessageBox.Show(ex.ToString());
return;
}
}
private MySession GetSession(string server, string logon, string password)
{
try
{
return new MySession(server, logon, password);
}
catch(Exception Ex)
{
throw(Ex);
}
}