Restoring the error handler

Restoring the error handler in the "toFile" function, even if an exception occurs. 
This is something we have to do manually if we catch the Exception somewhere else.