首页 > 解决方案 > 如何找到 map.at 坠毁的位置?

问题描述

我的代码崩溃了map.at(),这意味着它找到了一个不存在的密钥。

我在我的主要内容中发现了错误,但似乎只能捕捉到的信息

EXCEPTION: _Map_base::at

并且没有位置信息,这意味着我需要检查所有at功能。

有没有什么方法可以捕捉到这种错误并在它崩溃的地方打印出来?

这是我的代码:

#include <iostream>
using namespace std;

void func() {
  std::unordered_map<int, int> a;
  cout << a.at(2) << endl;
}

void main() {
  try {
    func();
  } catch (const std::exception& ex) { 
    printf("catch EXCEPTION: %s\n", ex.what());  // the mat at error catch here, How can i print the concrete location - func, in the message??????
  }
}

标签: c++try-catch

解决方案


推荐阅读