Issue #2252963 by Liam Morland: Fixed Improve error handling for file writing.