我有这个:
// static enum of supported HttpRequest to match requestToString
static const enum HttpRequest {
GET,
POST,
PUT,
DELETE,
OPTIONS,
HEAD,
TRACE
};
// typedef for the HttpRequests Map
typedef boost::unordered_map<enum HttpRequest, const char*> HttpRequests;
// define the HttpRequest Map to get static list of supported requests
static const HttpRequests requestToString = map_list_of
(GET, "GET")
(POST, "POST")
(PUT, "PUT")
(DELETE, "DELETE")
(OPTIONS,"OPTIONS")
(HEAD, "HEAD")
(TRACE, "TRACE");
现在如果我打电话
requestToString.at(GET);
没关系,但是如果我调用一个不存在的键
requestToString.at(THIS_IS_NO_KNOWN_KEY);
它给出了一个运行时异常并且整个过程中止..
防止这种情况的最佳方法是什么?是否有编译指示或什么,或者我应该用 try/catch 块或什么来“类 java”围绕它?
好心的亚历克斯