std :: map out_of_range异常未被捕获

时间:2017-05-28 19:34:58

标签: c++ exception-handling

我正在使用asio网络库。我看到一个非常奇怪的行为,调试器告诉我对std :: map :: at()的调用会抛出一个out_of_range异常,但是我有catch块来捕获这种类型的异常!

有问题的地图是:

import io
from PIL import Image

output = io.BytesIO()
with Image.open(image_path) as image:
  image.thumbnail((400, 400), Image.ANTIALIAS)
  image.save(output, format="JPEG")
  thumbnail_as_string = base64.b64encode(output.getvalue()).decode()

抛出异常的代码就是:

/*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

catch-block是否可以重新抛出并且调试器无法检测到? (我真的不这么认为。)

对不起,如果问题不清楚,我花了最后6个小时,我感到很绝望。

1 个答案:

答案 0 :(得分:0)

如果[]运算符不存在并且不会抛出,则cloud::subscribed[a_which].emplace_back(shared_from_this() ); 运算符将插入一个空值。

key

您可以使用find()检查是否存在if(cloud::subscribed.find(a_which) != cloud::subscribed.end())

void swap(void* arg1, void* arg2, int len) {
  char buff[len];
  memcpy(buff, arg1, len);
  memcpy(arg1, arg2, len);
  memcpy(arg2, buff, len);
}

最后,你有多么确定它会抛出一个超出范围的例外,而不是另一个例外?