1 year ago
#340734

donaldp
C# handled exception from Dropbox is coming up as unhandled exception
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($"********************************** 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($"********************************** {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;
}
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.
c#
.net
exception
dropbox-api
0 Answers
Your Answer