使用std::map时无法捕获未处理的异常
Unable to catch unhandled exception while using std::map
我正在尝试使用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()
的调用将抛出。
相关文章:
- 如何修复链表类实现的未处理异常0xDDDDDDDD
- VisualC++ 2010 有没有办法找出有关未处理异常错误的更多详细信息
- C++ 程序中双链表的未处理异常
- 运算符重载时未处理<<异常
- 实验室中0x6BE20E11 (ucrtbased.dll) 的未处理异常.exe: 0xC0000005:访问冲突读取
- ImageMagick 的未处理异常
- 类数组大小为的未处理异常
- 尝试将构造函数设置为 0 - c++ 的未处理异常
- Dijkstra 中0x001e1078 s_Algorithm.exe的未处理异常:0xC0000005:访问冲突读取位置0xbaadf00d
- 运算符重载未处理异常
- 尝试使用 fread 读取文本文件时发生 'System.AccessViolationException' 类型的未处理异常
- c++中的Guard,在main中未处理异常时不调用析构函数
- 某些内存位置的未处理异常
- 错误 linkedlist .exe 中0x00D0345E处的未处理异常: 0xC0000005:访问违规写入位置0x
- 仅在发布模式下使用 glGenBuffer 的未处理异常 - QT
- 如何分析VS2010在调试模式下捕获的未处理异常
- glDrawArrays上的openGL未处理异常(GL_TRIANGLES,0,model.indicesCount)
- 使用Boost Spirit解析语法的未处理异常
- 解析"%Z" C++ 中将错误作为访问 voilation 的未处理异常给出?
- 为什么在win32计时器回调中引发的未处理异常未被调试器视为未处理异常