使用boost::unknown_exception进行异常处理的示例程序

204 篇文章 ¥99.90 ¥299.90
本文展示了如何在C++中利用Boost库的boost::unknown_exception类处理未知异常。通过示例程序解释了如何在try-catch块中捕获并处理std::exception及非std::exception类型的异常,确保程序的健壮性。

使用boost::unknown_exception进行异常处理的示例程序

在使用C++进行开发时,异常处理是非常重要的一部分。Boost库提供了一个名为boost::unknown_exception的类,可以作为处理未知异常的备选方案。

下面是一个使用boost::unknown_exception处理异常的示例程序:

#include <boost/exception/all.hpp>
#include <iostream>

void foo()
{
    try {
        throw std::runtime_error("Hello, world!");
    }
    catch (...) {
        boost::throw_exception(
            boost::enable_error_info(boost::unknown_exception()));
    }
}

int main()
{
    try {
        foo();
    }
    catch (const std::exception& ex) {
        std::cerr << "Caught exception: " << ex.what() << std::endl;
    }
    catch (...) {
        std::cerr << "Caught unknown exception." << std::endl;
    }

    return 0;
}
int main(int argc, const char* argv[]) { utils::set_debug_mask(utils::DEBUG_LEVEL_ERROR | utils::DEBUG_LEVEL_INFO); if (utils::option_exists(argv, argv + argc, "-h")){ usage(argv[0]); return 0; } try { auto repeat = input::DEFAULT_REPEAT_TIME_MS; if (utils::option_exists(argv, argv + argc, "-r")) repeat = utils::option_get<unsigned int>(argv, argv + argc, "-r", true); string confdir = lirc::DEFAULT_CONF_DIR; if (utils::option_exists(argv, argv + argc, "-c")) confdir = utils::option_get<string>(argv, argv + argc, "-c", true); auto sample = lirc::SAMPLE_TIME_MS; if (utils::option_exists(argv, argv + argc, "-s")) sample = utils::option_get<unsigned int>(argv, argv + argc, "-s", true); if (!confdir.empty() && confdir.back() != '/') confdir += '/'; boost::asio::io_service io; boost::asio::io_service::work work(io); auto linux_lirc = std::make_shared<lirc>(io, confdir, lirc::DEFAULT_LIRC_DEVICE, sample); auto server = std::make_shared<stream_server>(io); server->start([&](msg::buffer req) { try { msg::reader<uint32_t> r(req); auto cmd = r.read<uint32_t>(); switch (cmd) { case GRASP_MSG_START_LEARN: { unsigned int key = r.read<uint32_t>(); auto i = keymap.find(key); if (i != keymap.end()) { oinfo << "start learn for key " << i->second << endl; linux_lirc->async_start_learn(i->second, [&](bool success) { oinfo << "IR learning succerr keu name:" << linux_lirc->get_key_name() << " succerr :"<<success<<endl; //server->write(msg::make_msg<uint32_t>({GRASP_MSG_LEARN_COMPLETE,success})); char tempStrLearning[32] = {0}; strcpy(tempStrLearning, linux_lirc->get_key_name().c_str()); msg::buffer msg_buf1; msg::push<uint32_t>(msg_buf1, GRASP_MSG_LEARN_COMPLETE); msg::push<uint32_t>(msg_buf1, success); msg::push(msg_buf1, tempStrLearning); server -> write(msg_buf1); }, [&]() { server->write(msg::make_msg<uint32_t>({GRASP_MSG_LEARN_ACTIVITY})); }); } else { oerr << "invalid key requested: " << key << endl; } break; } case GRASP_MSG_CANCEL_LEARN: linux_lirc->cancel(); server->write(msg::make_msg<uint32_t>({GRASP_MSG_LEARN_COMPLETE, 0})); break; case GRASP_MSG_CLEAR_LEARN: linux_lirc->delete_all_configs(); break; case GRASP_MSG_ONESELF_KEY: cout << "recv GRASP_MSG_ONESELF_KEY" << endl; linux_lirc->cancel(); learning_state = 0; break; default: oerr << "error: unknown grasp msg " << cmd << endl; break; } } catch (std::exception& e) { oerr << "exception: " << e.what() << std::endl; } }, GRASP_SOCKET_PATH); auto linux_input = std::make_shared<input_lirc>(io, repeat); /* * Jay L. 20170410 * This was the original call before I added the string, "remoteName". * * linux_input->start("lircd", [server](input_event ev) * * callback_t now uses KeyInfo_t which contains input_event and a string. * This has been changed here, and in a few places in "input_lirc". * * */ linux_input->start("lircd", [server](KeyInfo_t keyinfo) { if (keyinfo.ev.type == EV_KEY) { map<int, string> names = { {KEY_RELEASE, "release"}, {KEY_PRESS, "press"}, {KEY_REPEAT, "repeat"} }; oinfo << "key: " << keyinfo.ev.code << " value: " << names[keyinfo.ev.value] << " remote name: " << keyinfo.remoteName << endl; char tempStr[32] = {0}; strcpy(tempStr, keyinfo.remoteName.c_str()); msg::buffer msg_buf; msg::push<uint32_t>(msg_buf, GRASP_MSG_KEY_EVENT); msg::push<uint32_t>(msg_buf, keyinfo.ev.code); msg::push<uint32_t>(msg_buf, keyinfo.ev.value); msg::push(msg_buf, tempStr); server -> write(msg_buf); /* * Jay L. 20170410 * This was the original call before I added the string. Now we push * the individual elements of the message including the string. * server->write(msg::make_msg<uint32_t>({GRASP_MSG_KEY_EVENT, static_cast<uint32_t>(ev.code), static_cast<uint32_t>(ev.value)})); */ } }); boost::asio::signal_set signals(io); signals.add(SIGINT); signals.add(SIGTERM); signals.add(SIGQUIT); signals.async_wait( [&](boost::system::error_code /*ec*/, int signo) { oerr << "caught signal " << signo << std::endl; io.stop(); }); io.run(); } catch (std::exception& e) { oerr << "exception: " << e.what() << endl; } oinfo << "exiting" << endl; return 0; } 每行中文解释
最新发布
09-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编码实践

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值