146行目: 146行目:
     try {
     try {
       phoneNumberFormat();
       phoneNumberFormat();
    }
    catch (const std::exception &e) {
      std::cerr << "Main function error: " << e.what() << std::endl;
      return -1;
    }
    return 0;
}
</syntaxhighlight>
<br><br>
== 正規表現の使用例 : HTMLタグ ==
以下の例では、開始タグと終了タグの対応を確認しながら、HTMLタグの内容を抽出している。<br>
<br>
<syntaxhighlight lang="c++">
#include <iostream>
#include <regex>
#include <string>
void htmlTagExtraction()
{
    try {
      std::string html = "<div>Content</div><p>Paragraph</p>";
      std::regex pattern("<([a-zA-Z0-9]+)>([^<]+)</\\1>");
      std::smatch matches;
      std::string::const_iterator searchStart(html.cbegin());
      while (std::regex_search(searchStart, html.cend(), matches, pattern)) {
          std::cout << "Tag: " << matches[1] << ", Content: " << matches[2] << std::endl;
          searchStart = matches.suffix().first;
      }
    }
    catch (const std::regex_error &e) {
      std::cerr << "Regex error: " << e.what() << std::endl;
      std::cerr << "Code: " << e.code() << std::endl;
    }
    catch (const std::out_of_range &e) {
      std::cerr << "Out of range error: " << e.what() << std::endl;
    }
    catch (const std::exception &e) {
      std::cerr << "Error: " << e.what() << std::endl;
    }
}
int main()
{
    try {
      htmlTagExtraction();
     }
     }
     catch (const std::exception &e) {
     catch (const std::exception &e) {