Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The following are effectively the same (in outcome):</p> <pre><code>catch (Exception e) { FunctionA(e); FunctionB(e); } </code></pre> <p>But it's not the same if you do this:</p> <pre><code>catch (ApplicationException e) { FunctionB(e); } catch (Exception e) { FunctionA(e); } </code></pre> <p>This is because FunctionB gets typed to ApplicationException at compile time, wheras the call to FunctionA will always downcast the parameter to Exception. Depending on your implementation of FunctionB, this may not matter, but there are cases where it can make a difference. I would say that as a rule of thumb, if you your method implementation does not need the generic implementation, then don't use it.</p> <p>Here are some examples of when it would matter:</p> <pre><code>public T AddSomeContextToThisException&lt;T&gt;(T ex) where T : Exception { ex.Data.Add("Some key", "Some context message"); return ex; } public T ThisIsABadIdeaForExceptionsButMaybeAGoodIdeaForOtherTypes&lt;T&gt;(T ex) where T : Exception, new() { // do something with ex return new T(); } </code></pre> <p>The following example needs some additional code for context, so see the code beneath it:</p> <pre><code>private readonly Bar bar = new Bar(); public void HandExceptionOffToSomethingThatNeedsToBeStronglyTyped&lt;T&gt;(T ex) where T : Exception { ICollection&lt;T&gt; exceptions = bar.GetCollectionOfExceptions&lt;T&gt;(); exceptions.Add(ex); // do other stuff } public class Bar { // value is object, because .net doesn't have covariance yet private Dictionary&lt;Type, object&gt; listsOfExceptions = new Dictionary&lt;Type, object&gt;(); public ICollection&lt;T&gt; GetCollectionOfExceptions&lt;T&gt;() { if (!listsOfExceptions.ContainsKey(typeof(T))) { listsOfExceptions.Add(typeof(T), new List&lt;T&gt;()); } return listsOfExceptions[typeof(T)] as List&lt;T&gt;; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload