来自 Dropbox 的 C# 处理的异常将作为未处理的异常出现

发布于 2025-01-17 03:16:05 字数 3205 浏览 2 评论 0原文

我有一个 C#/.NET 应用程序,在 App.cs 中包含以下代码...

public App()
{
AppDomain.CurrentDomain.FirstChanceException+=CurrentDomain_FirstChanceException;
...
}

private void CurrentDomain_FirstChanceException(object sender, System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs e)
{
System.Diagnostics.Debug.WriteLine($"********************************** UNHANDLED EXCEPTION! Details: {e.Exception.ToString()}");
}

这运行良好。不过,我一直在使用 Dropbox CreateFolderV2Async,并且我想捕获任何“文件夹已存在”异常,但否则会重新抛出它。我的代码成功地捕获了这个,但是即使当它是那个确切的错误时我没有重新抛出它,它仍然显示在我的全局未处理的异常捕获器中,而且我不知道为什么 - 据我所知,它应该担心,是一个被捕获的错误,因此不会冒泡,但我看不到任何我错过的东西。

代码是...

public async Task<string> CreateFolderAsync(string Path,bool AutoRename=false)
{
CreateFolderResult result=null;
string folder=string.Empty;
try {
    result=await DxClient.Files.CreateFolderV2Async(Path,AutoRename).ConfigureAwait(false);
    folder=result.Metadata.PathDisplay;
    System.Diagnostics.Trace.WriteLine($"********************************** {folder} has been created\r\n");
    }
catch (Exception ex) {
    if (ex.Message.Contains("path/conflict/folder")) {
        System.Diagnostics.Trace.WriteLine($"********************************** {Path} already exists - skipping\r\n");
        folder="already exists";
    } else {
        System.Diagnostics.Trace.WriteLine($"********************************** Unhandled exception creating {Path}\r\n");
        throw;
        }
    }
return folder;
}

被这样调用(最初文件夹不存在,那么当然第二次调用它将存在)...

string testFolder="/TestFolder";
string createFolderResult=string.Empty;
createFolderResult=await DataService.CreateFolderAsync(testFolder);
await Task.Delay(5000);
createFolderResult=await DataService.CreateFolderAsync(testFolder);

我得到的输出是

********************************** /TestFolder has been created
Exception thrown: 'Dropbox.Api.ApiException`1' in Dropbox.Api.dll
********************************** UNHANDLED EXCEPTION! Details: Dropbox.Api.ApiException`1[Dropbox.Api.Files.CreateFolderError]: path/conflict/folder/.
   at Dropbox.Api.DropboxRequestHandler.Dropbox.Api.Stone.ITransport.SendRpcRequestAsync[TRequest,TResponse,TError](TRequest request, String host, String route, String auth, IEncoder`1 requestEncoder, IDecoder`1 responseDecoder, IDecoder`1 errorDecoder); Request Id: 7f73f60a3d294c87bfe49e47ab3c3bb2
Exception thrown: 'Dropbox.Api.ApiException`1' in System.Private.CoreLib.dll
********************************** UNHANDLED EXCEPTION! Details: Dropbox.Api.ApiException`1[Dropbox.Api.Files.CreateFolderError]: path/conflict/folder/.
   at Dropbox.Api.DropboxRequestHandler.Dropbox.Api.Stone.ITransport.SendRpcRequestAsync[TRequest,TResponse,TError](TRequest request, String host, String route, String auth, IEncoder`1 requestEncoder, IDecoder`1 responseDecoder, IDecoder`1 errorDecoder)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(); Request Id: 7f73f60a3d294c87bfe49e47ab3c3bb2
********************************** /TestFolder already exists - skipping

所以你可以从输出中看到确实已经存在的文件夹异常被捕获,但即使我不重新抛出它,它也会冒泡到我的全局未处理异常处理程序(这当然不是我想要的,因为我已经处理了它)。我在这里缺少什么?

谢谢, 唐纳德.

I have a C#/.NET app with the following code in the App.cs...

public App()
{
AppDomain.CurrentDomain.FirstChanceException+=CurrentDomain_FirstChanceException;
...
}

private void CurrentDomain_FirstChanceException(object sender, System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs e)
{
System.Diagnostics.Debug.WriteLine(
quot;********************************** UNHANDLED EXCEPTION! Details: {e.Exception.ToString()}");
}

This has been working well. However I've been working with the Dropbox CreateFolderV2Async, and I want to catch any "folder already exists" exception, but rethrow it otherwise. My code is successfully catching this, but even though I don't rethrow it when it's that exact error, it's still showing up in my global unhandled exception catcher, and I don't know why - it should, as far as I'm concerned, be a caught error, and therefore not bubble up, but I can't see anything that I'm missing.

The code is...

public async Task<string> CreateFolderAsync(string Path,bool AutoRename=false)
{
CreateFolderResult result=null;
string folder=string.Empty;
try {
    result=await DxClient.Files.CreateFolderV2Async(Path,AutoRename).ConfigureAwait(false);
    folder=result.Metadata.PathDisplay;
    System.Diagnostics.Trace.WriteLine(
quot;********************************** {folder} has been created\r\n");
    }
catch (Exception ex) {
    if (ex.Message.Contains("path/conflict/folder")) {
        System.Diagnostics.Trace.WriteLine(
quot;********************************** {Path} already exists - skipping\r\n");
        folder="already exists";
    } else {
        System.Diagnostics.Trace.WriteLine(
quot;********************************** Unhandled exception creating {Path}\r\n");
        throw;
        }
    }
return folder;
}

Being called as so (initially with the folder not existing, then of course the 2nd call it will exist)...

string testFolder="/TestFolder";
string createFolderResult=string.Empty;
createFolderResult=await DataService.CreateFolderAsync(testFolder);
await Task.Delay(5000);
createFolderResult=await DataService.CreateFolderAsync(testFolder);

And the output I get is

********************************** /TestFolder has been created
Exception thrown: 'Dropbox.Api.ApiException`1' in Dropbox.Api.dll
********************************** UNHANDLED EXCEPTION! Details: Dropbox.Api.ApiException`1[Dropbox.Api.Files.CreateFolderError]: path/conflict/folder/.
   at Dropbox.Api.DropboxRequestHandler.Dropbox.Api.Stone.ITransport.SendRpcRequestAsync[TRequest,TResponse,TError](TRequest request, String host, String route, String auth, IEncoder`1 requestEncoder, IDecoder`1 responseDecoder, IDecoder`1 errorDecoder); Request Id: 7f73f60a3d294c87bfe49e47ab3c3bb2
Exception thrown: 'Dropbox.Api.ApiException`1' in System.Private.CoreLib.dll
********************************** UNHANDLED EXCEPTION! Details: Dropbox.Api.ApiException`1[Dropbox.Api.Files.CreateFolderError]: path/conflict/folder/.
   at Dropbox.Api.DropboxRequestHandler.Dropbox.Api.Stone.ITransport.SendRpcRequestAsync[TRequest,TResponse,TError](TRequest request, String host, String route, String auth, IEncoder`1 requestEncoder, IDecoder`1 responseDecoder, IDecoder`1 errorDecoder)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(); Request Id: 7f73f60a3d294c87bfe49e47ab3c3bb2
********************************** /TestFolder already exists - skipping

So you can see from the output that indeed the already existing folder exception is being caught, but even though I don't rethrow it it's bubbling up to my global unhandled exception handler (which of course is not what I want, given I have handled it). What am I missing here?

Thanks,
Donald.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文