Alex Rivera | Logout

Where do I handle asynchronous exceptions?

Asked 2010-06-16T04:48:11.017
10

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?

Edit
Report

1 Answer

4

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

socket.EndConnection(result);

At that point the error in the BeginConnect process will be manifested in a thrown exception.

answered 2010-06-16T04:50:35.987

Your Answer