Sobes.tech
Junior — Senior

Reliable call of an asynchronous method: detecting exception handling issues

livecode

Task condition

There is an interface I with an asynchronous method DoThings() and a class F that attempts to call it "safely", i.e., without propagating exceptions outward. The current implementation contains several errors, which need to be identified.

class SafeExecutor
{
    public interface IService
    {
        Task DoThings();
    }

    public Task DoThingsSafely(IService service)
    {
        try
        {
            return service.DoThings();
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Handle exception: {ex.Message}");
            return Task.CompletedTask;
        }
    }
}
Reliable call of an asynchronous method: detecting… - sobes.tech