Question

I have a Azure worker role perform simple selects on a SQL Azure database. Rarely it throws the following SqlException.

Log

The underlying provider failed on Open. Inner Exception: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. Exception Type: System.Data.SqlClient.SqlException

The exception is not caught as a SqlException. It is caught in the generic exception handler. Any suggestions as to why that would be?

try{
}
catch(System.Data.SqlClient.SqlException sqlExcep)
{
}
catch(Exception genericExcep)
{
    **//The exception is caught as a generic exception**
}
Was it helpful?

Solution

The SQL Database environment is a layer of routers and proxies that handle network load balancing and resource management. If SQL Database itself didn't timeout, then something else in the middle could have (although that's typically rare).

I usually handle IOException errors as well and treat some of them as a form of transient error. What exception type are you actually receiving?

Did you try implementing the Transient Fault Handling Application Block? http://msdn.microsoft.com/en-us/library/hh680934(v=pandp.50)

Herve

OTHER TIPS

Looks like that's the inner exception, not the actual exception you caught. What is the type of the outer exception? The only thing you can do is catch whichever type the caught exception is, and then inspect the inner exception:

try
{
    // Stuff
}
catch (Exception exc)
{
    if (exc.InnerException is System.Data.SqlClient.SqlException)
    {
        var sqlException = exc.InnerException as System.Data.SqlClient.SqlException;

        // Do stuff with the error.
    }
}

The moral of the story is you can't explicitly catch the inner exception :(

Try Microsoft.Data.SqlClient.SqlException instead of System.Data.SqlClient.SqlException

        catch (Microsoft.Data.SqlClient.SqlException ex)
        {

        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top