I know it is not good practice to catch System.Exception unless on the top level of an application. What about a thread? Is it okay to catch System.Exception on the top level of the thread?
Update: The thread is a long running thread that should only be terminated when the application stops. So in order to make sure that the application does not crash I simply catch System.Exception and log the error. Everything is recreated.
while (Terminate == false)
{
var discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint());
try
{
var criteria = new FindCriteria(typeof(T));
criteria.Scopes.Add(new Uri(Scope));
var discovered = discoveryClient.Find(criteria);
discoveryClient.Close();
discoveryClient = null;
// do something with the endpoints
}
catch (OutOfMemoryException e)
{
m_Logger.LogException(e, "Exception when trying to discover clients (Contract: {0})", typeof(T).Name);
throw;
}
catch (Exception e)
{
m_Logger.LogException(e, "Exception when trying to discover clients (Contract: {0})", typeof(T).Name);
if (discoveryClient != null)
(discoveryClient as IDisposable).Dispose();
}
}