The following code is pretty clear, and my question is very simple:
Why does the AsyncCallback "HandleConnect" method not propagate the exception to the "Connect" method and how to distribute it?
public void Connect(IPEndPoint endpoint, IVfxIpcSession session) { try { ipcState.IpcSocket.BeginConnect(ipcState.IpcEndpoint, HandleConnect, ipcState); } catch(Exception x) { ManageException(x.ToString()); //Never Caught, though "HandleConnect" blows a SocketException } } private void HandleConnect(IAsyncResult ar) { // SocketException blows here, no propagation to method above is made. // Initially there was a try/catch block that hided it and this is NOT GOOD AT ALL // as I NEED TO KNOW when something goes wrong here. ipcState.IpcSocket.EndConnect(ar); }
1 - I think this is pretty normal behavior. But I would appreciate a comprehensive explanation of why this is happening and what is happening just behind the hoods.
2 - Is there any (quick and easy way) to throw an exception through my application?
forewarning I know that many dudes here are very critical, and I expect comments: βWhy don't you put the ManageException directly in theβ HandleConnect βmethod. Well, to make the long story short, let's just say:β I got my reasons. β I just posted a sample code here, and I want to extend this exclusive path further than this and do much more than shown elsewhere in the "N-upper" code.
EDIT
As a comment on a comment, I also tried this earlier, no luck:
private void HandleConnect(IAsyncResult ar) { try { ipcState.IpcSocket.EndConnect(ar); } catch(Exception x) { throw x;
My solution: I ended up creating an event handler to which each corresponding code logic is signed.
Thus, the exception is not just swallowed, but simply hits, but a notification is sent.
public event EventHandler<MyEventArgs> EventDispatch; private void HandleConnect(IAsyncResult ar) { try { ipcState.IpcSocket.EndConnect(ar); } catch(Exception x) { if (EventDispatch!= null) { EventDispatch(this, args); } } }
It's a little crooked, but it works great