处理自定义例外的课程
class for handling custom exception
我想创建一个符合std ::功能并允许处理指定异常的类,但我不确定是否可能。
这是伪草稿:
//exception types
template<class... Args>
class CustomExceptionHandler
{
public:
CustomExceptionHandler(std::function<void()> clb): clb_(std::move(clb)){}
void ExecuteCallback()
{
try
{
clb_();
}
/*catch specified exception types*/
}
private:
std::function<void()> clb_;
};
//usage
CustomExceptionHandler<std::out_of_range, std::overflow_error> handler(clb);
handler.ExecuteCallback();
我不知道如何使用variadic模板来获取异常类型并以后使用它。有可能吗?
我猜元组可能会有所帮助。
可能!我做了一个解决方案(您可以在此处运行),将异常类型的参数包扩展到一系列递归功能调用中,每个功能都尝试捕获一种类型的异常。最内向的递归电话然后调用回调。
namespace detail {
template<typename First>
void catcher(std::function<void()>& clb){
try {
clb(); // invoke the callback directly
} catch (const First& e){
// TODO: handle error as needed
std::cout << "Caught an exception with type "" << typeid(e).name();
std::cout << "" and message "" << e.what() << ""n";
}
}
template<typename First, typename Second, typename... Rest>
void catcher(std::function<void()>& clb){
try {
catcher<Second, Rest...>(clb); // invoke the callback inside of other handlers
} catch (const First& e){
// TODO: handle error as needed
std::cout << "Caught an exception with type "" << typeid(e).name();
std::cout << "" and message "" << e.what() << ""n";
}
}
}
template<class... Args>
class CustomExceptionHandler
{
public:
CustomExceptionHandler(std::function<void()> clb): clb_(std::move(clb)){}
void ExecuteCallback()
{
detail::catcher<Args...>(clb_);
}
private:
std::function<void()> clb_;
};
int main(){
std::function<void()> clb = [](){
std::cout << "I'm gonna barf!n";
throw std::out_of_range("Yuck");
//throw std::overflow_error("Ewww");
};
CustomExceptionHandler<std::out_of_range, std::overflow_error> handler(clb);
handler.ExecuteCallback();
return 0;
}
输出:
I'm gonna barf!
Caught an exception with type "St12out_of_range" and message "Yuck"
template<typename E0, typename ... En>
class ExceptionCatcher
{
public:
template<typename F>
void doit(F&& f)
{
try
{
ExceptionCatcher<En...> catcher;
catcher.doit(std::forward<F>(f));
}
catch(const E0 &)
{
std::cout << __PRETTY_FUNCTION__ << 'n';
}
}
};
template<typename E0>
class ExceptionCatcher<E0>
{
public:
template<typename F>
void doit(F&& f)
{
try
{
f();
}
catch(const E0 &)
{
std::cout << __PRETTY_FUNCTION__ << 'n';
}
}
};
https://wandbox.org/permlink/dauqtb9rwvmzt4b6
相关文章:
- 在自定义 std::vector-like 容器中处理指针和非指针模板类型的最佳方法是什么?
- boost::asio 中的自定义处理程序
- 如何处理自定义输出运算符中的 iomanips?
- 如何在Boost::program_options配置文件中为非字符串的自定义选项值类型处理空格
- 如何使用自定义对象的序列化在 c++ 中编写自定义二进制文件处理程序
- 处理许多自定义异常的最佳方法是什么
- 自定义迭代器:如果 a 和 b 的行为不同,如何正确处理距离计算和相等比较
- 智能指针自定义删除器中的处理条件
- 处理自定义QT小部件中的输入事件
- QlineEdit:显示一个处理过的文本,而不是输入的文本,而是保留它(自定义回声模式)
- glib:在另一个线程上处理自定义 GMainContext* 循环,不引发信号处理程序
- 使 ShowHint 使用多个不同的 Rect 处理自定义控件,每个 Rect 都有自己的 Hint
- C++自定义控制台类 - 如何处理运算符未重载"<<"的对象?
- 如何在抛出未处理的自定义异常后调用 what()?
- (C )正在创建专门用于处理所有其他自定义对象的类/对象一种处理项目的正确方法
- 处理自定义例外的课程
- 引发异常并直接结束程序的自定义异常处理
- 带有__PRETTY_UNCTION_的自定义错误处理程序
- QtWebkit:如何处理自定义协议
- 我可以为抛出bad_cast的情况安装一个自定义处理程序吗