Make sure file gets closed on exception.