<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: timyhac</title>
    <description>The latest articles on DEV Community by timyhac (@timyhac).</description>
    <link>https://dev.to/timyhac</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F213184%2Fab3cb84d-dd54-4d0b-a72e-4906d32452f4.png</url>
      <title>DEV Community: timyhac</title>
      <link>https://dev.to/timyhac</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/timyhac"/>
    <language>en</language>
    <item>
      <title>Reviewing re-throwing</title>
      <dc:creator>timyhac</dc:creator>
      <pubDate>Fri, 16 Aug 2019 08:59:49 +0000</pubDate>
      <link>https://dev.to/timyhac/reviewing-re-throwing-99o</link>
      <guid>https://dev.to/timyhac/reviewing-re-throwing-99o</guid>
      <description>&lt;h1&gt;
  
  
  TL;DR
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Implicit rethrow allows you to do something with the Exception, and then let it bubble up as if your code didn't touch it.&lt;/li&gt;
&lt;li&gt;Explicit rethrow mutates the Exception object before passing it, by resetting the stack trace&lt;/li&gt;
&lt;li&gt;In both cases, the Exception object is passed by reference.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;I've been dabbling in c# for a while, but I figured that my knowledge of python and javascript should have given me a bit of an edge. I'm certainly not starting from scratch, but I also wouldn't consider myself an expert in either of those languages.&lt;/p&gt;

&lt;p&gt;So, to prove to myself that I understand and can use c#, I've set myself a target to pass the &lt;a href="https://www.microsoft.com/en-us/learning/exam-70-483.aspx"&gt;70-483&lt;/a&gt; exam by the end of 2019.&lt;/p&gt;

&lt;p&gt;I decided to take a look around at previous exams, and stumbled upon &lt;a href="https://www.itexams.com/exam/70-483"&gt;this site&lt;/a&gt;. The first question I was given was around re-throwing an exception including it's original stack trace. The requirements were:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Log all exceptions by using the &lt;code&gt;LogException()&lt;/code&gt; method of the &lt;code&gt;ExceptionLogger&lt;/code&gt;class.&lt;/li&gt;
&lt;li&gt;Rethrow the original exception, including the entire exception stack.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It was a multiple choice question, and these were the options:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Option A&lt;/span&gt;
&lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;ExceptionLogger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LogException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Option B&lt;/span&gt;
&lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;ExceptionLogger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LogException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Option C&lt;/span&gt;
&lt;span class="k"&gt;catch&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;ExceptionLogger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LogException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Option D&lt;/span&gt;
&lt;span class="k"&gt;catch&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;ExceptionLogger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LogException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Straight away, I knew that Option C couldn't be it, because the ExceptionLogger was being passed a fresh new &lt;code&gt;Exception&lt;/code&gt; (clearly without any of the data of the original exception), and for that matter, didn't &lt;code&gt;catch&lt;/code&gt; have to have a parentheses to describe the &lt;code&gt;Exception&lt;/code&gt; type? It probably wouldn't even compile! (Later I discovered that it does...)&lt;/p&gt;

&lt;p&gt;It also couldn't be Option D because it clearly couldn't compile (&lt;code&gt;ex&lt;/code&gt; isn't defined anywhere), although to be honest I assumed it wouldn't compile because of the 'missing' parentheses on the &lt;code&gt;catch&lt;/code&gt; statement.&lt;/p&gt;

&lt;p&gt;So it was down to options A and B.&lt;/p&gt;

&lt;p&gt;I chose option B.&lt;/p&gt;

&lt;p&gt;The answer was option A.&lt;/p&gt;




&lt;p&gt;In retrospect, I wasn't really sure what &lt;code&gt;throw&lt;/code&gt; without an exception did. I had seen it before, but I guess I thought that it wouldn't be any different to &lt;code&gt;throw ex&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Option B is called an &lt;strong&gt;Explicit&lt;/strong&gt; rethrow, whereas Option A is called an &lt;strong&gt;Implicit&lt;/strong&gt; rethrow. In most situations you want to use an implicit rethrow because it preserves the stack, and it turns out that this is a &lt;a href="https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2012/ms182363(v=vs.110)"&gt;well known rule&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;But my goal is to deeply understand, and not just to blindly copy rules, so I went about writing some code to test what was going on...&lt;/p&gt;




&lt;h2&gt;
  
  
  Iteration 1
&lt;/h2&gt;

&lt;p&gt;To verify that the answer was actually correct, I decided to create some code that would let me see what happens when throwing an exception explicitly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;DivideByZeroException&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;ex1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ex1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// System.DivideByZeroException ... Line 13&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="n"&gt;ex1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// or throw(ex1);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;ex2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ex2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// System.DivideByZeroException ... Line 18&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The console prints that there is a &lt;code&gt;DivdeByZeroException&lt;/code&gt; on lines 13 and 18.&lt;/p&gt;

&lt;p&gt;Even though I had read through the aforementioned articles, it was still surprising! After all, the exception happens only on line 13.&lt;/p&gt;

&lt;p&gt;So I removed the explicit rethrow from the catch block to see whether what they said was actually true:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;DivideByZeroException&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;ex1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ex1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// System.DivideByZeroException ... Line 13&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;ex2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ex2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// System.DivideByZeroException ... Line 13&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Both &lt;code&gt;WriteLine()&lt;/code&gt; calls showed that the exception was on line 13.&lt;/p&gt;

&lt;h2&gt;
  
  
  Iteration 2
&lt;/h2&gt;

&lt;p&gt;There must be a new Exception being created, I reasoned, that is why the second WriteLine() call shows the exception on line 18!&lt;/p&gt;

&lt;p&gt;So I modified my code to the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;try&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;DivideByZeroException&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;ex1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;A&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ex1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="n"&gt;ex1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;ex2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;B&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ex2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// System.DivideByZeroException ... Line 15&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// System.DivideByZeroException ... Line 15&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now &lt;em&gt;both&lt;/em&gt; &lt;code&gt;WriteLine()&lt;/code&gt; calls show the exception to be falling on line 20, not on line 15 (now the location of the &lt;code&gt;DivideByZeroException&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;I was shocked!&lt;/p&gt;

&lt;p&gt;After grappling with this for a while, I realised that when we throw an exception, the exception object is passed to the catch statement by reference, not by value. Therefore, A and B were pointing to the same object.&lt;br&gt;
I could quickly check this theory by looking at whether the objects were equal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;    &lt;span class="c1"&gt;// Output: True&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In hindsight, this seems obvious - but it was critical to my understanding of what was going on here.&lt;/p&gt;

&lt;p&gt;So the only difference between my previous iteration and this one was the point at which the exception was serialized to text; before the throw, and after the throw.&lt;/p&gt;

&lt;p&gt;Exception A was being passed to the second catch block by being the parameter to the throw statement, and this was what was mutating it:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Takeaway:&lt;/strong&gt; An explicit throw actually changes the Exception object by resetting it's stack trace.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://www.dotnetjalps.com/2013/10/throw-vs-throw-ex-csharp.html"&gt;This blog post&lt;/a&gt; by Jalpesh Vadjama also helped in my understanding of the inner workings.&lt;/p&gt;

</description>
      <category>csharp</category>
    </item>
  </channel>
</rss>
