Exception safe resource management