title: How can I catch out of range error for map.at error in c++?
body: <p>i want to use map to save my data.</p>
  <p>because my function is a member function, and i marked it as const.</p>
  <p>So, when i want to get value, i can only use <code>map.at(key)</code></p>
  <p>but when the key is missing , i will get a <code>const std::out_of_range &amp; e</code> error.</p>
  <p>For this case, i design a macro:</p>
  <pre><code>#include &lt;map&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string&gt;
  
  #define START_MAP try {
  #define END_MAP(s) \
  } catch (const std::out_of_range &amp; e) {\
    printf(&quot;%s map at crashed\n&quot;, s.c_str());\
    exit(1);\
  }\
  
  int main() {
    std::map&lt;std::string, int&gt; m;
    START_MAP;
    std::string b = &quot;test&quot;;
    int a = m.at(b);
    END_MAP(&quot;&quot;); // TODO: how can i get b here??
  }
  </code></pre>
  <p>but i want to know the key name in my printf message, how can i get it?</p>