<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Windows Exception Handling]]></title><description><![CDATA[Windows Exception Handling]]></description><link>https://rxnveera.blog</link><generator>RSS for Node</generator><lastBuildDate>Sat, 11 Apr 2026 05:10:42 GMT</lastBuildDate><atom:link href="https://rxnveera.blog/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Windows Exception Handling]]></title><description><![CDATA[Agenda :
what is exception ?
what causes an exception ?
what are the type of exception handler exist ?
who sees it first ?
What windows does by default ?
when you need to register handlers ?
What is windows exception (at OS level) ?

An exception is ...]]></description><link>https://rxnveera.blog/windows-exception-handling</link><guid isPermaLink="true">https://rxnveera.blog/windows-exception-handling</guid><category><![CDATA[windows-internals]]></category><dc:creator><![CDATA[rxnveera]]></dc:creator><pubDate>Mon, 22 Dec 2025 07:08:46 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1766386908797/9b18adf9-4e29-4b64-9bbd-0d056356e327.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><strong>Agenda</strong> :</p>
<p>what is exception ?</p>
<p>what causes an exception ?</p>
<p>what are the type of exception handler exist ?</p>
<p>who sees it first ?</p>
<p>What windows does by default ?</p>
<p>when you need to register handlers ?</p>
<p><strong>What is windows exception (at OS level) ?</strong></p>
<ul>
<li><p>An exception is an event that stops the normal execution of code</p>
</li>
<li><p>It typically originates from one of two places :</p>
<ul>
<li><p>Hardware (the CPU)</p>
</li>
<li><p>Software</p>
</li>
</ul>
</li>
</ul>
<p>    <strong>Hardware (CPU)</strong> : The CPU tries to execute an instruction but cannot</p>
<p>    <strong>Example</strong> : Access Violation , Integer Divide by Zero</p>
<ul>
<li><pre><code class="lang-c">      <span class="hljs-keyword">int</span>* ptr = <span class="hljs-literal">nullptr</span>; <span class="hljs-comment">// Pointer is NULL (address 0) </span>
      *ptr = <span class="hljs-number">42</span>; <span class="hljs-comment">// CRASH! Writing to address 0 is illegal</span>
</code></pre>
<p>  The CPU executes the instruction to write 42 to address 0x0. The CPU hardware refuses and triggers an interrupt. The Windows Kernel catches this interrupt and converts it into an Exception Code (like 0xC0000005).</p>
<p>  <strong>Software</strong> : Your program explicitly tells the OS to trigger an exception using the windows API function RaiseException().</p>
</li>
</ul>
<p><strong>The Exception Handling Hierarchy (Order of Precedence) :</strong></p>
<p>When that exception occurs, Windows looks for a handler in a specific order. It does not go straight to your try/catch block immediately.</p>
<p><strong>Order of Precedence :</strong></p>
<ol>
<li><p><strong>Debugger (if attached)</strong> : If a debugger is attached , windows pauses execution and lets the debugger know --&gt; this is called as the first chance exception</p>
</li>
<li><p><strong>Vectored Exception Handler (VEH)</strong> : if no debugger handles it (or none is attached), windows pauses the execution and calls VEH</p>
<ol>
<li><p><strong>Priority</strong> : Highest priority within the application.</p>
</li>
<li><p><strong>Scope</strong> : Global (Process-wide). It handles exceptions from any thread.</p>
</li>
<li><p><strong>Registration</strong> : You must manually register this using <strong>AddVectoredExceptionHandler.</strong></p>
</li>
</ol>
</li>
<li><p><strong>Structured Exception Handling</strong> :</p>
<ol>
<li><p><strong>Priority</strong> : Lower than VEH</p>
</li>
<li><p><strong>Scope</strong> : Local (Thread based and stack based) , It looks for try , catch , except blocks in your specific function and then checks the function that called it , and so on (unwinding the stack)</p>
</li>
</ol>
</li>
<li><p><strong>UnHandledExceptionFilter</strong> : if no SEH handles it , the system calls a top level filter</p>
</li>
<li><p><strong>Default system handler</strong> : if nothing else handles it , windows takes over .</p>
</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1766387195228/e57daef1-3326-4169-b4c7-95c8d6ef4b21.png" alt class="image--center mx-auto" /></p>
<p><strong>Do we need to register the handlers (or) windows do this by default ?</strong> we need to register the handlers only if we want to prevent the application from getting crashed or log the error before dying. If we are okay with the program closing when it crashes, you do not need to write any handling code.</p>
<p>If you do not register any handlers (VEH or SEH) and your code crashes: 1. Windows invokes the Default Exception Handler. 2. This handler typically collects crash data (Windows Error Reporting). 3. It displays the "Application has stopped working" dialog or simply kills the process silently. 4. The application exits immediately. It does not recover.</p>
<p><strong>Examples :</strong></p>
<p><strong>0x01 - The Default Behavior : No Handler</strong></p>
<p>This program will just crash and close.</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">int</span>* badPtr = <span class="hljs-literal">nullptr</span>;
    *badPtr = <span class="hljs-number">10</span>; <span class="hljs-comment">// Exception triggered here by CPU</span>
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p><strong>0x02 - Structured Exception handler</strong></p>
<p>This is the standard way to handle errors in specific blocks of code</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;windows.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
    __try {
        <span class="hljs-comment">// Protected code block</span>
        <span class="hljs-keyword">int</span>* badPtr = <span class="hljs-literal">nullptr</span>;
        *badPtr = <span class="hljs-number">10</span>; 
    }
    __except (EXCEPTION_EXECUTE_HANDLER) {
        <span class="hljs-comment">// Windows found this handler on the stack!</span>
        <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"Caught an Access Violation! The program is saved."</span> &lt;&lt; <span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;
    }

    <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"Program continues execution..."</span> &lt;&lt; <span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p><strong>0x03 - Vectored exception Handler</strong></p>
<p>This handler is called before the SEH block above, even if the crash happens deep inside a function.</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;windows.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;</span></span>

<span class="hljs-comment">// Your custom VEH function</span>
<span class="hljs-function">LONG WINAPI <span class="hljs-title">MyVectoredHandler</span><span class="hljs-params">(PEXCEPTION_POINTERS pExceptionInfo)</span> </span>{
    <span class="hljs-keyword">if</span> (pExceptionInfo-&gt;ExceptionRecord-&gt;ExceptionCode == EXCEPTION_ACCESS_VIOLATION) {
        <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"[VEH] I saw the crash FIRST (Global Handler)!"</span> &lt;&lt; <span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;

        <span class="hljs-comment">// OPTIONAL: You can try to fix it, or tell Windows to keep searching.</span>
        <span class="hljs-comment">// EXCEPTION_CONTINUE_SEARCH tells Windows to let SEH (the next handler) try.</span>
        <span class="hljs-keyword">return</span> EXCEPTION_CONTINUE_SEARCH; 
    }
    <span class="hljs-keyword">return</span> EXCEPTION_CONTINUE_SEARCH;
}

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-comment">// Register the VEH</span>
    AddVectoredExceptionHandler(<span class="hljs-number">1</span>, MyVectoredHandler);

    __try {
        <span class="hljs-keyword">int</span>* badPtr = <span class="hljs-literal">nullptr</span>;
        *badPtr = <span class="hljs-number">10</span>; 
    }
    __except (EXCEPTION_EXECUTE_HANDLER) {
        <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"[SEH] Caught locally."</span> &lt;&lt; <span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;
    }

    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p><strong>Output of the VEH example:</strong></p>
<p>I saw the crash FIRST (Global Handler)! (VEH runs first) Caught locally. (Because VEH returned CONTINUE_SEARCH, SEH got a chance).</p>
<p><strong>Want to dive deeper into Windows internals?</strong></p>
<p><strong>Check out my other blog posts for more deep dives!</strong></p>
]]></content:encoded></item></channel></rss>