How can I catch multiple Exceptions in my code? Like I have the following code for Delete
operation, I want to catch Exception for REFERENCE constraint
and for SqlConnection
Exception.
public void DeleteProduct(Product p)
{
try
{
using (IDbCommand cmd = dbConnection.CreateCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = SpDeleteProduct;
dbConnection.Open();
cmd.ExecuteNonQuery();
}
}
catch (Exception ex)
{
System.ServiceModel.Web.WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;
throw new FaultException(new FaultReason(new FaultReasonText(ex.Message)));
}
}
Then in my client code I want to check the type of Exception thrown so I can show a personalized message to the user:
void DeleteProductCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Error != null)
{
FaultException fault = e.Error as FaultException;
//Something like
// if e.Error == SqlConnection Exception
GetExceptionMessage("Error occured in connecting to DB");
// if e.Error == Refeence constraint Exception
GetExceptionMessage("foreign key violation");
}
}
Is it possible?