0

以下示例来自教程。当我运行它时,它会抛出异常,然后是 coredump。我尝试使用 catch() 来捕获异常以避免 coredump,如下所示:

但它不起作用。有什么建议么?

谢谢

-托德

----核心转储消息开始---

在抛出一个实例后调用终止

'boost::exception_detail::clone_impl >'

what():重复运算符“ ”不能启动正则表达式。解析正则表达式时发生错误:'>>>HERE>>> '。

中止(核心转储)

- - 结尾 - -

--- 程序开始 ----

#include <boost/regex.hpp>
#include <iostream>
    void print_captures(const std::string& regx, const std::string& text)

    {

    boost::regex e(regx);

   boost::smatch what;

   std::cout << "Expression:  \"" << regx << "\"\n";

   std::cout << "Text:        \"" << text << "\"\n";

   try {

     //     boost::regex_match(text, what, e, boost::match_extra);

     boost::regex_match(text, e);

   }

   **catch(boost::regex_error& e) {

std::cout <<"!!!!\n"; 

   } 

   catch (...) {

     std::cout << "###\n"; 

   }** 



}


int main(int , char* [])

{
  print_captures("*", "AAA");

 } 

- - 结尾 -

4

1 回答 1

1

boost::regex()抛出异常的是它的构造函数:

boost::regex e(regx);

将它放在try块中,它将被boost::regex_error&异常处理程序捕获。

于 2012-03-16T08:17:30.563 回答