Catch exception while writing files