1

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.

13
  • Are you sure that the "/path/conflict/folder" string is in the Message property of the exception? There are other members like InnerException that also contain text.
    – jonsca
    Commented Mar 27, 2022 at 8:08
  • @jonsca The catch "if" statement is certainly catching it (as per the output which shows that code is executing). Are you saying it could be another exception within that? If so what would I need to do to catch that too?
    – donaldp
    Commented Mar 27, 2022 at 8:59
  • Okay, but don't you want it to catch the unhandled ones first? It seems like you're looking for that particular string "/path/conflict/folder" in the unhandled ones, and I'm wondering if it's missing those because that message is not part of ex.Message but in ex.InnerException instead. I admit the order that the exceptions are coming back is puzzling.
    – jonsca
    Commented Mar 27, 2022 at 9:12
  • What happens when you step through that method?
    – jonsca
    Commented Mar 27, 2022 at 9:14
  • 1
    That stinks. Sorry I could not have been of more help!
    – jonsca
    Commented Mar 28, 2022 at 2:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.