Consider the following code:
class Foo {
// boring parts omitted
private TcpClient socket;
public void Connect(){
socket.BeginConnect(Host, Port, new AsyncCallback(cbConnect), quux);
}
private void cbConnect(IAsyncResult result){
// blah
}
}
If socket
throws an exception after BeginConnect
returns and before cbConnect
gets called, where does it pop up? Is it even allowed to throw in the background?
Code sample of exception handling for asynch delegate from msdn forum. I beleive that for TcpClient pattern will be the same.
If the process of accepting a connection results in an error your cbConnect method will be called. To complete the connection though you'll need to make the following call
At that point the error in the BeginConnect process will be manifested in a thrown exception.