C# 从Azure表捕获错误的干净方法(字符串匹配除外?)

C# 从Azure表捕获错误的干净方法(字符串匹配除外?),c#,azure,try-catch,azure-table-storage,C#,Azure,Try Catch,Azure Table Storage,我想获得所有Azure表错误的列表,并找出一种干净的方法来处理try…catch块中的错误 例如,我不想直接编码InnerException消息并将其与String.Contains(“指定的实体已经存在”)进行比较。捕捉这些错误的正确方法是什么 您可以尝试查看响应中的值,而不是内部异常。这是我的一个try-catch块的示例: try { return query.FirstOrDefault(); } catch (System.Data.Services.Client.DataSe

我想获得所有Azure表错误的列表,并找出一种干净的方法来处理
try…catch
块中的错误

例如,我不想直接编码InnerException消息并将其与
String.Contains(“指定的实体已经存在”)
进行比较。捕捉这些错误的正确方法是什么


您可以尝试查看响应中的值,而不是内部异常。这是我的一个try-catch块的示例:

try {
    return query.FirstOrDefault();
}
catch (System.Data.Services.Client.DataServiceQueryException ex)
{
    if (ex.Response.StatusCode == (int)System.Net.HttpStatusCode.NotFound) {
        return null;
    }
    throw;
}

显然,这只是针对“项目不存在”错误,但我相信您可以通过查看来扩展此概念。

请参见此处的代码:。模式是捕获StorageClientException,然后使用.ErrorCode属性与StorageErrorCode中的常量进行匹配。

以下是中提供的代码,但我不确定这是否比smark的回复有任何价值

   /*
         From Azure table whitepaper

         When an exception occurs, you can extract the sequence number (highlighted above) of the command that caused the transaction to fail as follows:

try
{
    // ... save changes 
}
catch (InvalidOperationException e)
{
    DataServiceClientException dsce = e.InnerException as DataServiceClientException;
    int? commandIndex;
    string errorMessage;

    ParseErrorDetails(dsce, out commandIndex, out errorMessage);
}


          */
-


要在向表中添加对象时处理错误,可以使用以下代码:

try {
  _context.AddObject(TableName, entityObject);
  _context.SaveCangesWithRetries(); 
}
catch(DataServiceRequestException ex) {
  ex.Response.Any(r => r.StatusCode == (int)System.Net.HttpStatusCode.Conflict) 
  throw;
}

正如在其他答案中所说,您可以在以下位置找到表存储错误列表:

谢谢!通常与以下哪些异常关联:context.RetryPolicy。。超时。。你要继续吗。。使用后隧道。。。合并选项。。。servicepoint.ConnectionLimit?当创建健壮的应用程序时,一个指南将非常有用。似乎该表将抛出“System.Data.Services.Client.DataServiceQueryException”,而不是您博客上提到的“StorageClientException”。这将更改我的处理程序实现。。。至于什么,我还不确定。
try {
  _context.AddObject(TableName, entityObject);
  _context.SaveCangesWithRetries(); 
}
catch(DataServiceRequestException ex) {
  ex.Response.Any(r => r.StatusCode == (int)System.Net.HttpStatusCode.Conflict) 
  throw;
}