Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
168 views
in Technique[技术] by (71.8m points)

c# - Exceptions are not received in caller when using ASYNC

I am using DispatcherTimer to process a method at a specified interval of time

dispatcherTimer = new DispatcherTimer()
{
   Interval = new TimeSpan(0, 0, 0, 1, 0)
};
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);

Here is the dispatcherTimer_Tick method

private void dispatcherTimer_Tick(object sender, EventArgs e)
{
    try
    {
        Task.Run(() => MethodWithParameter(message));
    }
    catch (Exception ex)
    {        
    }
}

Here I am calling MQTTPublisher which is a DLL reference.

private async static void MethodWithParameter(string message)
{
    try
    {
        await MQTTPublisher.RunAsync(message);    
    }
    catch (Exception Ex)    
    {       
    }            
}

I am not able to catch the exceptions which are thrown in that DLL. How can I get exception to caller?

Definition of RunAsync - This is in separate dll.

public static async Task RunAsync(string message)
{
    var mqttClient = factory.CreateMqttClient();
    //This creates MqttFactory and send message to all subscribers
    try
    {
        await mqttClient.ConnectAsync(options);        
    }
    catch (Exception exception)
    {
        Console.WriteLine("### CONNECTING FAILED ###" + Environment.NewLine + exception);
        throw exception;
    }
}

And

Task<MqttClientConnectResult> ConnectAsync(IMqttClientOptions options)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is the downside of using async void. Change your method to return async Task instead :

private async static Task MethodWithParameter(string message)
{
    try
    {
        await MQTTPublisher.RunAsync(message);

    }
    catch (Exception Ex)    
    {

    }            
}

Based on: Async/Await - Best Practices in Asynchronous Programming

Async void methods have different error-handling semantics. When an exception is thrown out of an async Task or async Task method, that exception is captured and placed on the Task object. With async void methods, there is no Task object, so any exceptions thrown out of an async void method will be raised directly on the SynchronizationContext that was active when the async void method started.

And:

Figure 2 Exceptions from an Async Void Method Can’t Be Caught with Catch

private async void ThrowExceptionAsync()
{
    throw new InvalidOperationException();
}

public void AsyncVoidExceptions_CannotBeCaughtByCatch()
{
    try
    {
        ThrowExceptionAsync();
    }
    catch (Exception)
    {
        // The exception is never caught here!
        throw;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...