The problem is that you're trying to catch SqlException
, but that's not the exception type: it's the InnerException.
If user10954641 is right, and its an UpdateException
, you could simply catch that:
try
{
}
catch (UpdateException e)
{
}
If it's a different exception, then catch whatever exception type is actually thrown. Alternatively, you could conditionally catch exceptions where the InnerException is SqlException
:
try
{
}
catch (Exception e) when (e.InnerException is SqlException)
{
}
If you're wondering why catch (SqlException)
doesn't work, consider the following code:
public void ThrowException()
{
try
{
MakeDatabaseCall();
}
catch (Exception e)
{
throw new Exception("Uh oh!", e);
}
}
public void MakeDatabaseCall()
{
throw new SqlException();
}
A call to ThrowException()
will ultimately produce an error form MakeDatabaseCall()
, but the exception type will not be SqlException
because we're wrapping it in another exception (in this case the exception base class): Exception
. But, because we're passing the exception thrown by MakeDatabaseCall()
into the constructor, our "Uh oh!" exception will contain the SqlException
in its InnerException
field.
When you use try
/catch(ExceptionType e)
, you're instructing .NET to pattern match the exception before deciding which catch
statement to enter. For the sake of illustration, the two pieces of code below are roughly equivalent:
try
{
}
catch(SqlException e)
{
//catches an SqlException
}
and
try
{
}
catch(Exception e) when (e is SqlException)
{
//catches an SqlException
}
Clearly in our ThrowException
example, Exception
is not derived from SqlException
, so catch (SqlException e)
won't match.