Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot catch exception thrown by File.WriteAllBytesAsync #491

Closed
JerrettDavis opened this issue Jul 8, 2019 · 4 comments
Closed

Cannot catch exception thrown by File.WriteAllBytesAsync #491

JerrettDavis opened this issue Jul 8, 2019 · 4 comments

Comments

@JerrettDavis
Copy link
Contributor

When attempting to use IFile.WriteAllBytesAsync, all underlying exceptions from File.WriteAllBytesAsync are quietly disregarded. I can see the exceptions being thrown, but a try/catch fails to catch the exceptions. Instead, the program continues execution on the next line. I believe this is due to the FileWrapper not awaiting the response from File.WriteAllBytes, and instead it's always returning a Task.Completed task.

@robkeim
Copy link
Contributor

robkeim commented Jul 8, 2019

@JerrettDavis in order to catch exceptions thrown by async methods you need to await their results like this:

Task<Result> resultTask = someMethodThatReturnsAResultByThrowsAnException();
// No exception is thrown at this point
// Continue doing other work...
try
{
    Result result = await resultTask; // this will now throw an exception
}
catch (Exception e)
{
    // Handle your exception here
}

Here you can see a quick fiddle that I threw together showing this behavior:
https://dotnetfiddle.net/7jj6lJ

@JerrettDavis
Copy link
Contributor Author

JerrettDavis commented Jul 8, 2019

@robkeim Ordinarily, that would work, but it doesn't appear to work properly for WriteAllBytesAsync.

Here is an example method:

public async Task<string> SaveFileAsync(string path, byte[] file, string fileType, CancellationToken cancellationToken = default)
{
    var filename = _filenameProvider.GetFilename(path, fileType);
    var fullPath = _fileSystem.Path.Combine(path, filename)?.Replace('/', '\\');

    _fileSystem.Directory.CreateDirectory(path);
    try
    {
        await _fileSystem.File.WriteAllBytesAsync(fullPath, file, cancellationToken);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }

    return filename;
}

The catch block is never triggered, but if I switch the method over to WriteAllBytes it works as desired.

image
You can also see the exception being thrown in the Debug output window.

And here is an example fiddle.

@fgreinacher
Copy link
Contributor

This is fixed with https://github.com/System-IO-Abstractions/System.IO.Abstractions/releases/tag/v6.0.15, should be on NuGet.org soon

@JerrettDavis
Copy link
Contributor Author

It's live on nuget, and the fix is confirmed working in my original use-case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants