3

So I'm receiving a Win32Exception during an Entity Framework "Any" transaction. The db machine is offline so it's to be expected. It's a monitoring app so I want to log that the machine isn't available however it's not catching the error.

The error is: (reported by Elmah)

System.ComponentModel.Win32Exception The network path was not found

System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) ---> System.ComponentModel.Win32Exception (0x80004005): The network path was not found at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)

Ok, so here's my (pseudo) code:

if(Monitor.TryEnter(Variables))
try
{
    foreach(var thing in things)
    {
        try
        {
            using(var ctx = new context())
            {
                Do Stuff... 
                if(thing.Any()) <-- Error
                Do more stuff... like look for problems.
            }
        }
        catch(Win32Exception)
        {
            Add exception to list of problems.;
        }
        catch(exception)
        {
            throw; //I was just seeing if this would help, it didn't
        }
     }
     Return list of problems
}
catch(Exception ex)
{
    Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(ex));
}
finally
{
    Monitor.Exit(variables)
}

So I hope you can see that the Win32Exception should be caught, but it isn't - Elmah is reporting it. The errors that are being found in the database are being located and reported upon properly. It all works until this Win32Exception happens then it just seems to ignore my catch.

Tod
  • 2,070
  • 21
  • 27
  • But ELMAH report shows that it is System.Data.SqlClient.SqlException at the top of stack trace. Try to catch that instead. – Eldarien Sep 08 '15 at 08:31
  • Ok, I just noticed that, seems weird that it's reported as a Win32Exception of a SQLException. Makes little sense to me, I'll give it a go though. Thanks. – Tod Sep 08 '15 at 08:34

0 Answers0