-
Notifications
You must be signed in to change notification settings - Fork 257
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
Comments
@JerrettDavis in order to catch exceptions thrown by async methods you need to await their results like this:
Here you can see a quick fiddle that I threw together showing this behavior: |
@robkeim Ordinarily, that would work, but it doesn't appear to work properly for 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
|
This is fixed with https://github.com/System-IO-Abstractions/System.IO.Abstractions/releases/tag/v6.0.15, should be on NuGet.org soon |
It's live on nuget, and the fix is confirmed working in my original use-case. |
When attempting to use
IFile.WriteAllBytesAsync
, all underlying exceptions fromFile.WriteAllBytesAsync
are quietly disregarded. I can see the exceptions being thrown, but atry/catch
fails to catch the exceptions. Instead, the program continues execution on the next line. I believe this is due to theFileWrapper
not awaiting the response fromFile.WriteAllBytes
, and instead it's always returning a Task.Completed task.The text was updated successfully, but these errors were encountered: