在我的每個主要函數中,我想捕獲某些類的異常並將它們轉換爲退出代碼。一個隱含的try {}抓住主
有沒有比這個更優雅的解決方案比起始和結束每個主要功能的宏都會粘貼隱含的try {} catch
我想要的?
我可以通過std::set_terminate
功能以某種方式實現此功能嗎?
實施例:
int main(){
try { //<- insert this
/*
the business logic goes here
*/
//-> and insert this
}
catch(const Someclass1& e){ return 2; }
catch(const Someclass2& e){ return 3; }
//...
catch(...){ return 1; }
}
是否所有你的例外與繼承有關(或者他們可以?) – quamrana
@quamrana是的。它實際上可能只是關於一個虛擬基類。 – PSkocik
然後,您可以讓每個異常類嵌入返回碼,並且'main()'中的一個try/catch可以提取並返回它。 – quamrana