1

I am using a third party DLL which offers an async method to perform an operation.

I run this inside a try-catch block but - with JustMyCode disabled - I get an error originating from somewhere inside the third party object which can only be handled in AppDomain.CurrentDomain.UnhandledException, which closes the app and is far to generalised as a place to handle this.

The code:

try { ResultObject result = await thirdPartyObject.MethodAsync(); }
catch { /* never get here */ }

The signature of the thirdPartyObject.MethodAsync() from reflection:

public Task<ResultObject> MethodAsync();

After hitting the internal error (which is only visible with 'Just My Code' disabled in VS settings), it goes onto a System.Threading.Tasks.TaskCanceledException: 'A task was canceled.' ...

Is there a way to intercept that task cancellation without hitting the global unhandled exception handler?

7
  • thirdPartyObject.MethodAsync().Result; will do? Commented Jan 19, 2018 at 15:44
  • I've seen something similar with angularjs async http calls and deferred caches. You should contact the devs of the third party, maybe send a pull request. Commented Jan 19, 2018 at 15:45
  • I don't see a reason why that would not enter the catch block unless their code is bad which could be since their object is named "ResultObject". Commented Jan 19, 2018 at 15:46
  • @Crowcoder funnily enough the method name is renamed for this question, so don't get hung up on the method name. This is more about the exception behaviour. Commented Jan 19, 2018 at 15:47
  • 2
    Can you comb through their decompiled code and see if they are calling any async void methods? Commented Jan 19, 2018 at 15:50

1 Answer 1

4

Is there a way to intercept that task cancellation without hitting the global unhandled exception handler?

Not without catching the exception in the third-party code. If the third-party method for example starts a new thread, or a new Task which it doesn't await, and an exception is thrown on this background thread, it won't get catched by your catch clause code.

Please refer to @Peter Torr's blog post for more information and some examples: https://blogs.msdn.microsoft.com/ptorr/2014/12/10/async-exceptions-in-c/

You should really ask the manufacturer of the assembly to fix their code.

Sign up to request clarification or add additional context in comments.

2 Comments

This is correct - there is no way to solve this without fixing the 3rd party code (which has now been done, happily!)
I'm getting exception from mono runtime in http secure handling. Would be nice, If I can prevent app crash. github.com/mono/mono/issues/8350

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.