cleanup some exception safetly and raii