[英]std::map out_of_range exception not being caught
我正在使用asio網絡庫。 我看到一個非常奇怪的行為,調試器告訴我對std :: map :: at()的調用引發out_of_range異常,但是我有catch塊來精確捕獲這種類型的異常!
有問題的地圖是這樣的:
/*Both of these maps are global variables in the namespace cloud*/
map<string, weak_ptr<session> > cloud::services;
map<string, vector<weak_ptr<session> > > cloud::subscribed; //this one
引發異常的代碼是這樣的:
void session::subscribirse(std::string a_which)
{
try
{
//We obtain a reference to the group of sockets subscribed to this service name
vector<weak_ptr<session>>& grupo = cloud::subscribed.at(a_which); //HERE
grupo.emplace_back(shared_from_this() );
}
catch(out_of_range& e) //The group didn't exist (no-one had subscribed to it yet)
{
vector<weak_ptr<session>> new_group;
new_group.emplace_back(shared_from_this());
cloud::subscribed.emplace(make_pair(a_which, new_group));
}
catch(...)
{
cout << "unexpected exception during subscribe\n";
}
subscriptions_.emplace_back(a_which);
consumed_ = true;
}
捕獲塊是否可以重新拋出,調試器無法檢測到? (我真的不這么認為)。
抱歉,如果問題不清楚,我已經呆了6個小時,感到很絕望。
[]
運算符將插入一個空值(如果該值尚不存在且不會拋出)。
cloud::subscribed[a_which].emplace_back(shared_from_this() );
您可以使用find()
檢查key
是否存在:
if(cloud::subscribed.find(a_which) != cloud::subscribed.end())
最后,您如何確定它拋出了異常,而不是另一個異常?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.