为什么最终尝试后我的退货声明不可用?

Gle*_*rie 0 c# unreachable-code

这是我的代码。我有一种删除Azure Block Blob的方法。我有一个try / finally块来记录失败的详细信息,否则请继续进行。

我将返回,bool以标识该项目是否已删除。

Visual Studio告诉我return方法末尾的语句不可访问。

public bool Delete(string referenceId)
{
    var client = GetBlobClient();
    var container = client.GetContainerReference("qwerty");
    var blob = container.GetBlockBlobReference(referenceId);
    try
    {
        blob.Delete();
        return true;
    }
    finally
    {
        Trace.TraceWarning("Delete Blob failed: {0}", referenceId);
    }
    return false;  // <-- THIS LINE.
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

这是为什么?我不必在此方法中返回一些值。

感谢您的见解。

Jon*_*ase 5

您没有任何收获,因此,如果尝试中有异常,它将被抛出。您的方法成功并返回true,否则将引发异常。

如果添加捕获块,警告将消失,因为现在可以访问该路径。

您只需catch {}在finally之前添加,就应该能够吞下异常(如果这就是您想要的)。

在你的代码仔细看,你可能想更换finally一个catchanyways-它不可能你希望记录的删除未能不管结果如何。