【发布时间】:2015-09-11 15:46:28
【问题描述】:
在我的程序顶部,我有一个异常处理程序。
它看起来像这样:
try{
//majority of program
}
catch(...){
Handle_All_Exceptions();
}
void Handle_All_Exceptions(){
try{throw;}
catch(TypeA const& e){Handle(e) ;}
catch(TypeB const& e){Handle(e) ;}
catch(TypeC const& e){Handle(e) ;}
catch(TypeD const& e){Handle(e) ;}
catch(...) {Handle_Unknown();}
}
void Handle(TypeA const& e){
//...
}
void Handle(TypeB const& e){
//...
}
void Handle(TypeC const& e){
//...
}
void Handle(TypeD const& e){
//...
}
void Handle_Unknown(){
//...
}
随着我获得更多的异常类型,
我想采用更通用的方法。
如何将通用编程应用于 Handle_All_Exceptions 函数?
在较新版本的 C++ 中是否会出现这样的情况?
catch(auto e){Handle(e)};
catch(...){Handle_Unknown();}
【问题讨论】:
-
你的异常有共同的基类吗?
-
@robert,不,但我认为继承 std::exception 可能是更好的方法。
-
@TrevorHickey 建议您为基类考虑 std::runtime_error:en.cppreference.com/w/cpp/error/runtime_error
标签: c++ templates try-catch c++-concepts c++17