我稍稍改变了标题,因为我认为这是更恰当的问题.
你会重构吗(好像合法使用goto)?
如果,你如何重构以下代码来删除转到语句?
if (data.device) { try { ... } catch(const std::exception&) { goto done; } ... // more things which should not be caught done: ; }
完整的声明
#ifdef HAVE_GPU // attempt to use GPU device if (data.device) { try { Integral::Gpu eri(S,R,Q,block.shell()); eri(basis.centers(),quartets,data.device); } // if GPU fails,propagate to cpu catch(std::exception) { goto done; } data.device += size; host_index.extend(block_index); block_index.data.clear(); done: ; } #endif
谢谢
之后看到了大多数的偏好,我决定和旗一起去,但是约克先生的评论.
谢谢大家
解决方法
if (data.device) { bool status = true; try { ... } catch(std::exception) { status = false; } if (status) { ... // more things which should not be caught } }