I’ve an class TCPDataLink implementing some TCP features using Socket. I also have an IOManager which does some logging when an error is thrown (on all my DataLink).
The problem is that I cannot find a way to catch exceptions of Socket event handlers from my IOManager Class. I do not want to inject Logger dependency inside my TCPDataLink class since I don’t wan’t to repeat the logging code in all my DataLink classes.
Example in a normal case :
// IOManager Class call in a normal case
try
{
tcpInstance.DoSomething
}
catch (MyCustomEx e)
{
// Log the problem
Logger.log(e.ToString());
}
In my problematic case :
// TCPDataLink.cs
...
socket.OnConnection += ConnectionHandler // Cannot try/catch or whatever
...
void ConnectionHandler(...)
{
// Code throwing Exceptions
}
According to this thread, external try/catch is not an option. So I’ve to catch Exception inside my ConnectionHanlder and… ?
What’s my best alternative to log from my IOManager whitout the possibility to catch Exception ?
Thanks,
The best way is to catch excpetion actually. Orchestrating your app workflow with expected exceptions handling is sometihng that you normally do in IO interactions, often there is no other way to handle that correctly.
So just handle it within your
ConnectionHandlerwith sometry/catch/throwsequence.May be, do not raise an exception one time it catched, but just handle it.