<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="julia&gt; corrupt_stacktrace() = error(&quot;destroy stacktrace&quot;)

julia&gt; f() = error(&quot;expected&quot;)
f (generic function with 1 method)

julia&gt; try
        f()
       finally
        try; corrupt_stacktrace(); end
       end
ERROR: 
Stacktrace:
 [1] error at ./error.jl:33 [inlined]
 [2] corrupt_stacktrace at ./REPL[8]:1 [inlined] # should have been f!
 [3] macro expansion at ./REPL[14]:4 [inlined]
 [4] top-level scope at ./&lt;missing&gt;:0"><pre class="notranslate">julia<span class="pl-k">&gt;</span> <span class="pl-en">corrupt_stacktrace</span>() <span class="pl-k">=</span> <span class="pl-c1">error</span>(<span class="pl-s"><span class="pl-pds">"</span>destroy stacktrace<span class="pl-pds">"</span></span>)

julia<span class="pl-k">&gt;</span> <span class="pl-en">f</span>() <span class="pl-k">=</span> <span class="pl-c1">error</span>(<span class="pl-s"><span class="pl-pds">"</span>expected<span class="pl-pds">"</span></span>)
f (generic <span class="pl-k">function</span> with <span class="pl-c1">1</span> method)

julia<span class="pl-k">&gt;</span> <span class="pl-k">try</span>
        <span class="pl-c1">f</span>()
       <span class="pl-k">finally</span>
        <span class="pl-k">try</span>; <span class="pl-c1">corrupt_stacktrace</span>(); <span class="pl-k">end</span>
       <span class="pl-k">end</span>
ERROR<span class="pl-k">:</span> 
Stacktrace<span class="pl-k">:</span>
 [<span class="pl-c1">1</span>] error at <span class="pl-k">./</span>error<span class="pl-k">.</span>jl<span class="pl-k">:</span><span class="pl-c1">33</span> [inlined]
 [<span class="pl-c1">2</span>] corrupt_stacktrace at <span class="pl-k">./</span>REPL[<span class="pl-c1">8</span>]<span class="pl-k">:</span><span class="pl-c1">1</span> [inlined] <span class="pl-c"><span class="pl-c">#</span> should have been f!</span>
 [<span class="pl-c1">3</span>] <span class="pl-k">macro</span> expansion at <span class="pl-k">./</span>REPL[<span class="pl-c1">14</span>]<span class="pl-k">:</span><span class="pl-c1">4</span> [inlined]
 [<span class="pl-c1">4</span>] top<span class="pl-k">-</span>level scope at <span class="pl-k">./</span><span class="pl-k">&lt;</span><span class="pl-c1">missing</span><span class="pl-k">&gt;:</span><span class="pl-c1">0</span></pre></div>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="function foo()
    try
        error(&quot;error in foo&quot;)
    catch e
    end
end

function bar()
    try
        error(&quot;error in bar&quot;)
    catch e
        foo()
        rethrow(e)
    end
end

bar()"><pre class="notranslate"><span class="pl-k">function</span> <span class="pl-en">foo</span>()
    <span class="pl-k">try</span>
        <span class="pl-c1">error</span>(<span class="pl-s"><span class="pl-pds">"</span>error in foo<span class="pl-pds">"</span></span>)
    <span class="pl-k">catch</span> e
    <span class="pl-k">end</span>
<span class="pl-k">end</span>

<span class="pl-k">function</span> <span class="pl-en">bar</span>()
    <span class="pl-k">try</span>
        <span class="pl-c1">error</span>(<span class="pl-s"><span class="pl-pds">"</span>error in bar<span class="pl-pds">"</span></span>)
    <span class="pl-k">catch</span> e
        <span class="pl-c1">foo</span>()
        <span class="pl-c1">rethrow</span>(e)
    <span class="pl-k">end</span>
<span class="pl-k">end</span>

<span class="pl-c1">bar</span>()</pre></div>
<p dir="auto">produces the misleading</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="ERROR: error in bar
Stacktrace:
 [1] foo() at ./REPL[1]:3
 [2] bar() at ./REPL[2]:5"><pre class="notranslate">ERROR<span class="pl-k">:</span> error <span class="pl-k">in</span> bar
Stacktrace<span class="pl-k">:</span>
 [<span class="pl-c1">1</span>] <span class="pl-c1">foo</span>() at <span class="pl-k">./</span>REPL[<span class="pl-c1">1</span>]<span class="pl-k">:</span><span class="pl-c1">3</span>
 [<span class="pl-c1">2</span>] <span class="pl-c1">bar</span>() at <span class="pl-k">./</span>REPL[<span class="pl-c1">2</span>]<span class="pl-k">:</span><span class="pl-c1">5</span></pre></div>
<p dir="auto">This just hit me in a really wicked situation: The stacktrace actually lead me to a line erroneously throwing the reported exception. I fixed it (to throw the correct exception), but the same error kept showing, leaving me completely puzzled for quite some time...</p>