writeTextFile: add exception handler