使用std::map时无法捕获未处理的异常

Unable to catch unhandled exception while using std::map

本文关键字:未处理 异常 std map 使用      更新时间:2023-10-16

我正在尝试使用std::map中的operator[]来使用键读取元素。

但是,当我试图访问一个无效的密钥时,它抛出了一个异常,我无法使用try-catch块捕获该异常。

这是我正在使用的代码:

class MapElement
{
 public:
     int a;
     char c;
};
int main()
{
   MapElement m1,m2,m3;
   m1.a =10;m1.c = 'a';
   m2.a =20;m2.c ='b';
   m3.a =30;m3.c ='c';
   map <char ,MapElement*> Mymap;
   map<char,MapElement*>::iterator iter = Mymap.begin();
   Mymap.insert(iter , std::pair<int, MapElement*>('1',&m1));
   Mymap.insert(iter , std::pair<int, MapElement*>('1',&m2));
   cout<<Mymap['1']->a;
   try
   {
      cout<<Mymap['2']->a;
   }
   catch(exception e)
   {
       cout<<e.what();
   }
   catch(...)
   {
        cout<< "unknown error";
   }
}

如何处理此异常?

问题是由std::map::operator[]为不存在的密钥创建新条目引起的:

返回对映射到与键等效的键的值的引用,如果该键不存在,则执行插入。

在这种情况下,该值是一个指针,不会指向有效的MapElement。这不是运行时故障,而是程序员错误,导致了未定义的行为。即使有可能捕捉到这种类型的错误,也不应该以允许程序继续的方式捕捉,因为程序可能会表现出其他意外行为。

如果编译器支持c++11:,请使用std::map::at()

try
{
    std::cout<< Mymap.at('2') << std::endl;
}
catch (std::out_of_range& const e)
{
    std::cerr << e.what() << std::endl;
}

(参见http://ideone.com/FR4svY例如)。否则,如果编译器不支持c++11,请使用std::map::find(),它不会抛出异常,但如果映射不包含请求的密钥,则返回std::map::end()

template <typename K, typename V>
V& map_at(std::map<K, V>& a_map, K const& a_key)
{
    typename std::map<K, V>::iterator i = a_map.find(a_key);
    if (a_map.end() == i)
    {
        throw std::out_of_range("map_at()");
    }
    return i->second;
}
try
{
    std::cout<< map_at(Mymap, '2') << std::endl;
}
catch (std::out_of_range& const e)
{
    std::cerr << e.what() << std::endl;
}

(参见http://ideone.com/lIkTD3例如)。

我建议您使用find方法,并将它们与地图的端到端键进行比较:

map<char,MapElement*>::iterator iter = Mymap.find('2');
if (iter != Mymap.end()) {
    // do something if the key exist
} else {
    // do anythig if the key was not founded
}

问题是您在这里取消了对空指针的引用,因为使用不存在的键调用operator[]会导致使用该键和值初始化值类型(在本例中为空MapElement*)创建一个新元素:

cout<<Mymap['2']->a; // The error is to call ->a, not Mymap['2']

这不会引发异常,而是未定义的行为。您可以调用一个引发异常的方法:

MapElement* m = Mymap.at('2'); // throws if there is no '2' element
cout << m->a;

这里,如果没有关键字为'2'的元素,则对at()的调用将抛出。