<spanclass=cp>#define JSON_CATCH_USER(exception) </span><spanclass=cm>/* value */</span><spanclass=cp></span>
<spanclass=c1>// (2)</span>
<spanclass=cp>#define JSON_THROW_USER(exception) </span><spanclass=cm>/* value */</span><spanclass=cp></span>
<spanclass=c1>// (3)</span>
<spanclass=cp>#define JSON_TRY_USER </span><spanclass=cm>/* value */</span><spanclass=cp></span>
</code></pre></div><p>Controls how exceptions are handled by the library.</p><ol><li>This macro overrides <ahref=https://en.cppreference.com/w/cpp/language/try_catch><codeclass=highlight><spanclass=k>catch</span><spanclass=w></span></code></a> calls inside the library. The argument is the type of the exception to catch. As of version 3.8.0, the library only catches <code>std::out_of_range</code> exceptions internally to rethrow them as <ahref=../../../home/exceptions/#out-of-range><code>json::out_of_range</code></a> exceptions. The macro is always followed by a scope.</li><li>This macro overrides <codeclass=highlight><spanclass=k>throw</span><spanclass=w></span></code> calls inside the library. The argument is the exception to be thrown. Note that <code>JSON_THROW_USER</code> should leave the current scope (e.g., by throwing or aborting), as continuing after it may yield undefined behavior.</li><li>This macro overrides <codeclass=highlight><spanclass=k>try</span><spanclass=w></span></code> calls inside the library. It has no arguments and is always followed by a scope.</li></ol><h2id=parameters>Parameters<aclass=headerlinkhref=#parameterstitle="Permanent link">¶</a></h2><dl><dt><code>exception</code> (in)</dt><dd>an exception type</dd></dl><h2id=default-definition>Default definition<aclass=headerlinkhref=#default-definitiontitle="Permanent link">¶</a></h2><p>By default, the macros map to their respective C++ keywords:</p><divclass=highlight><pre><span></span><code><spanclass=cp>#define JSON_CATCH_USER(exception) catch(exception)</span>
</code></pre></div><p>When exceptions are switched off, the <codeclass=highlight><spanclass=k>try</span><spanclass=w></span></code> block is executed unconditionally, and throwing exceptions is replaced by calling <ahref=https://en.cppreference.com/w/cpp/utility/program/abort><code>std::abort</code></a> to make reaching the <codeclass=highlight><spanclass=k>throw</span><spanclass=w></span></code> branch abort the process.</p><divclass=highlight><pre><span></span><code><spanclass=cp>#define JSON_THROW_USER(exception) std::abort()</span>
<spanclass=cp>#define JSON_TRY_USER if (true)</span>
<spanclass=cp>#define JSON_CATCH_USER(exception) if (false)</span>
</code></pre></div><h2id=examples>Examples<aclass=headerlinkhref=#examplestitle="Permanent link">¶</a></h2><detailsclass=example><summary>Example</summary><p>The code below switches off exceptions and creates a log entry with a detailed error message in case of errors.</p><divclass=highlight><pre><span></span><code><spanclass=cp>#include</span><spanclass=w></span><spanclass=cpf><iostream></span><spanclass=cp></span>