1

I want to test if my try/catch block in the extension method TimeoutAfter catches an exception if there is an exception in the line await task;.

But something is wrong, because the try/catch block doesn't catch the exception and my application crashes with this exception in this line:

UIApplication.Main(args, null, "AppDelegate");

System.NullReferenceException has been thrown Object reference not set to an instance of an object

I get System.NullReferenceException immediately after the line await task; was executed. What is wrong with my try/catch block?

namespace LeaderboardTest
{
[Register("AppDelegate")]
class Program : UIApplicationDelegate
{
    private static Game1 game;

    internal static void RunGame()
    {
        game = new Game1();
        game.Run();
    }

    static void Main(string[] args)
    {
        UIApplication.Main(args, null, "AppDelegate");
    }

    public override void FinishedLaunching(UIApplication app)
    {
        global::Xamarin.Forms.Forms.Init();
        RunGame();
    }
}
}

I call the extension method TimeoutAfter like this:

bool playertask = false;
playertask = await Extensionmethods.TimeoutAfter(UpdatePlayerCountryData("Germany", "Berlin"), new TimeSpan(0, 0, 0, 5, 0));

private async Task UpdatePlayerCountryData(string country, string city)
{
    var resultprofile = await PlayFabClientAPI.UpdateUserDataAsync(new PlayFab.ClientModels.UpdateUserDataRequest()
    {
        Data = new Dictionary<string, string>() {
            {"Country", country},
            {"City", city}
            },
        Permission = PlayFab.ClientModels.UserDataPermission.Public
    });

    if (resultprofile.Error != null)
        Console.WriteLine(resultprofile.Error.GenerateErrorReport());
    else
    {
        Console.WriteLine("Successfully updated user data");
        throw new Exception("Test Exception"); // my test exception
    }
}

The extension method TimeoutAfter where I want to catch the exception:

namespace LeaderboardTest
{
public static class Extensionmethods
{
    public static async Task<bool> TimeoutAfter(this Task task, TimeSpan timeout)
    {
        using (var timeoutCancellationTokenSource = new CancellationTokenSource())
        {
            var completedTask = await Task.WhenAny(task, Task.Delay(timeout, timeoutCancellationTokenSource.Token));
            if (completedTask == task)
            {
                timeoutCancellationTokenSource.Cancel();
                try
                {
                    await task;
                    return true;
                }
                catch (Exception ex)
                {
                    var message = "Task Exception Message: " + ex.Message;
                    var innermessage = "Task Inner Exception Message: " + ex.InnerException.Message;
                    return false;
                }
            }
            else
            {
                var messagetimeout = "The operation has timed out.";
                return false;
            }
        }
    }
}
}
Tobey60
  • 137
  • 5

0 Answers0