<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: The TryThis method</title>
	<atom:link href="http://lostechies.com/johnteague/2009/08/27/the-trythis-method/feed/" rel="self" type="application/rss+xml" />
	<link>http://lostechies.com/johnteague/2009/08/27/the-trythis-method/</link>
	<description></description>
	<lastBuildDate>Wed, 08 May 2013 18:43:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
	<item>
		<title>By: Scott Koon</title>
		<link>http://lostechies.com/johnteague/2009/08/27/the-trythis-method/#comment-96</link>
		<dc:creator>Scott Koon</dc:creator>
		<pubDate>Thu, 27 Aug 2009 16:16:15 +0000</pubDate>
		<guid isPermaLink="false">/blogs/johnteague/archive/2009/08/27/the-trythis-method.aspx#comment-96</guid>
		<description>Yeah, we do something like this:

        protected T WrapWithStandardExceptionHandling&lt;T&gt;(int eventLogId, Func&lt;T&gt; func)
        {
            try
            {
                var result = func();
                return result;
            }
            catch (Exception e)
            {
                if (!(typeof(BaseException).IsAssignableFrom(e.GetType())))
                {
                    Log.Instance.LogError(eventLogId, e);
                }
                throw;
            }
        }</description>
		<content:encoded><![CDATA[<p>Yeah, we do something like this:</p>
<p>        protected T WrapWithStandardExceptionHandling<t>(int eventLogId, Func</t><t> func)<br />
        {<br />
            try<br />
            {<br />
                var result = func();<br />
                return result;<br />
            }<br />
            catch (Exception e)<br />
            {<br />
                if (!(typeof(BaseException).IsAssignableFrom(e.GetType())))<br />
                {<br />
                    Log.Instance.LogError(eventLogId, e);<br />
                }<br />
                throw;<br />
            }<br />
        }</t></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike</title>
		<link>http://lostechies.com/johnteague/2009/08/27/the-trythis-method/#comment-95</link>
		<dc:creator>Mike</dc:creator>
		<pubDate>Thu, 27 Aug 2009 14:32:23 +0000</pubDate>
		<guid isPermaLink="false">/blogs/johnteague/archive/2009/08/27/the-trythis-method.aspx#comment-95</guid>
		<description>For this cross-cutting code I tend to use AOP using Castle Windsor for instance by convention all public methods on my controllers will have an interceptor assigned to them to handle logging of any unhandled exceptions http://code.google.com/p/issuetrackerdotnet/source/browse/trunk/src/IssueTracker.Controllers/ExceptionLoggingInterceptor.cs

This way your code does not need to change at all no lamdas needed!</description>
		<content:encoded><![CDATA[<p>For this cross-cutting code I tend to use AOP using Castle Windsor for instance by convention all public methods on my controllers will have an interceptor assigned to them to handle logging of any unhandled exceptions <a href="http://code.google.com/p/issuetrackerdotnet/source/browse/trunk/src/IssueTracker.Controllers/ExceptionLoggingInterceptor.cs" rel="nofollow">http://code.google.com/p/issuetrackerdotnet/source/browse/trunk/src/IssueTracker.Controllers/ExceptionLoggingInterceptor.cs</a></p>
<p>This way your code does not need to change at all no lamdas needed!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jcteague</title>
		<link>http://lostechies.com/johnteague/2009/08/27/the-trythis-method/#comment-94</link>
		<dc:creator>jcteague</dc:creator>
		<pubDate>Thu, 27 Aug 2009 14:06:55 +0000</pubDate>
		<guid isPermaLink="false">/blogs/johnteague/archive/2009/08/27/the-trythis-method.aspx#comment-94</guid>
		<description>@pablo
I don&#039;t test private methods directly, I just put the method in my test class for the purpose of this demonstration.

Usually I&#039;ll will test the public methods that call the private method and make sure I get all of the paths with NCover.

You can test protected methods by creating a test double.  I&#039;ll do a post about that tonight.</description>
		<content:encoded><![CDATA[<p>@pablo<br />
I don&#8217;t test private methods directly, I just put the method in my test class for the purpose of this demonstration.</p>
<p>Usually I&#8217;ll will test the public methods that call the private method and make sure I get all of the paths with NCover.</p>
<p>You can test protected methods by creating a test double.  I&#8217;ll do a post about that tonight.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jcteague</title>
		<link>http://lostechies.com/johnteague/2009/08/27/the-trythis-method/#comment-93</link>
		<dc:creator>jcteague</dc:creator>
		<pubDate>Thu, 27 Aug 2009 14:03:58 +0000</pubDate>
		<guid isPermaLink="false">/blogs/johnteague/archive/2009/08/27/the-trythis-method.aspx#comment-93</guid>
		<description>@Micheal, @Nick
I don&#039;t use production code in my blog posts.  I&#039;m trying to illustrate how you can use delegates to clean up you code.  In reality I am catching a specific exception.  As some of the other commenters have mentioned, if you want something more generic there are ways.

@David, I started with a Func paramter, but because of the different  number of input paramters didn&#039;t work very well.  That&#039;s why I encapsulated it in a action where I can handle any number of paramters, different output parameters easily.

@Tuna, I like that.  I thought about doing something a little more fluentish.  Maybe next time.</description>
		<content:encoded><![CDATA[<p>@Micheal, @Nick<br />
I don&#8217;t use production code in my blog posts.  I&#8217;m trying to illustrate how you can use delegates to clean up you code.  In reality I am catching a specific exception.  As some of the other commenters have mentioned, if you want something more generic there are ways.</p>
<p>@David, I started with a Func paramter, but because of the different  number of input paramters didn&#8217;t work very well.  That&#8217;s why I encapsulated it in a action where I can handle any number of paramters, different output parameters easily.</p>
<p>@Tuna, I like that.  I thought about doing something a little more fluentish.  Maybe next time.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott Koon</title>
		<link>http://lostechies.com/johnteague/2009/08/27/the-trythis-method/#comment-92</link>
		<dc:creator>Scott Koon</dc:creator>
		<pubDate>Thu, 27 Aug 2009 12:39:07 +0000</pubDate>
		<guid isPermaLink="false">/blogs/johnteague/archive/2009/08/27/the-trythis-method.aspx#comment-92</guid>
		<description>We use a similar method called WrapWithStandardExceptionHandling in our service layer, but we use it in a lambda. I can&#039;t remember the specific implementation details right now though, it&#039;s 5 AM here, I&#039;ll look at it at work and see if it might have the same effect.</description>
		<content:encoded><![CDATA[<p>We use a similar method called WrapWithStandardExceptionHandling in our service layer, but we use it in a lambda. I can&#8217;t remember the specific implementation details right now though, it&#8217;s 5 AM here, I&#8217;ll look at it at work and see if it might have the same effect.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Thibault</title>
		<link>http://lostechies.com/johnteague/2009/08/27/the-trythis-method/#comment-91</link>
		<dc:creator>David Thibault</dc:creator>
		<pubDate>Thu, 27 Aug 2009 12:17:09 +0000</pubDate>
		<guid isPermaLink="false">/blogs/johnteague/archive/2009/08/27/the-trythis-method.aspx#comment-91</guid>
		<description>Nice idea. Also :

public static class Try {
  public static bool This&lt; T &gt;(Func&lt; T &gt; block, out T result) {
    try {
      result = block();
      return true;
    }
    catch (Exception) {
      result = default(T);
      return true;
    }
  }
}

And then :

int result;
if (Try.This(() =&gt; MightGoWrong(), out result)) {
  // it worked and we have the result :)
}</description>
		<content:encoded><![CDATA[<p>Nice idea. Also :</p>
<p>public static class Try {<br />
  public static bool This< T >(Func< T > block, out T result) {<br />
    try {<br />
      result = block();<br />
      return true;<br />
    }<br />
    catch (Exception) {<br />
      result = default(T);<br />
      return true;<br />
    }<br />
  }<br />
}</p>
<p>And then :</p>
<p>int result;<br />
if (Try.This(() => MightGoWrong(), out result)) {<br />
  // it worked and we have the result <img src='http://lostechies.com/johnteague/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: pablo</title>
		<link>http://lostechies.com/johnteague/2009/08/27/the-trythis-method/#comment-90</link>
		<dc:creator>pablo</dc:creator>
		<pubDate>Thu, 27 Aug 2009 09:30:16 +0000</pubDate>
		<guid isPermaLink="false">/blogs/johnteague/archive/2009/08/27/the-trythis-method.aspx#comment-90</guid>
		<description>My tests are always located in a separated project that contains ONLY the NUnit test classes.

The problem that I&#039;m facing with it is that I cannot test protected/private methods. 

How would you test your protected/private methods?</description>
		<content:encoded><![CDATA[<p>My tests are always located in a separated project that contains ONLY the NUnit test classes.</p>
<p>The problem that I&#8217;m facing with it is that I cannot test protected/private methods. </p>
<p>How would you test your protected/private methods?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tuna Toksoz</title>
		<link>http://lostechies.com/johnteague/2009/08/27/the-trythis-method/#comment-89</link>
		<dc:creator>Tuna Toksoz</dc:creator>
		<pubDate>Thu, 27 Aug 2009 09:22:10 +0000</pubDate>
		<guid isPermaLink="false">/blogs/johnteague/archive/2009/08/27/the-trythis-method.aspx#comment-89</guid>
		<description>My favorite is:

Try.Something(()=&gt;doexceptionalthinghere())
      .Catch&lt;Exception&gt;(x=&gt;dosomethingwithx)

i don&#039;t use this at all, but like it anyway :)</description>
		<content:encoded><![CDATA[<p>My favorite is:</p>
<p>Try.Something(()=>doexceptionalthinghere())<br />
      .Catch<exception>(x=>dosomethingwithx)</p>
<p>i don&#8217;t use this at all, but like it anyway <img src='http://lostechies.com/johnteague/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </exception></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nick</title>
		<link>http://lostechies.com/johnteague/2009/08/27/the-trythis-method/#comment-88</link>
		<dc:creator>Nick</dc:creator>
		<pubDate>Thu, 27 Aug 2009 08:40:38 +0000</pubDate>
		<guid isPermaLink="false">/blogs/johnteague/archive/2009/08/27/the-trythis-method.aspx#comment-88</guid>
		<description>This seems like a good solution to check that *an* exception has been thrown.  The only problem is that if you want to be testing your exceptional cases to see what type of exception it was or what the message was, you can&#039;t do it with this.</description>
		<content:encoded><![CDATA[<p>This seems like a good solution to check that *an* exception has been thrown.  The only problem is that if you want to be testing your exceptional cases to see what type of exception it was or what the message was, you can&#8217;t do it with this.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tim Van Wassenhove</title>
		<link>http://lostechies.com/johnteague/2009/08/27/the-trythis-method/#comment-87</link>
		<dc:creator>Tim Van Wassenhove</dc:creator>
		<pubDate>Thu, 27 Aug 2009 07:00:20 +0000</pubDate>
		<guid isPermaLink="false">/blogs/johnteague/archive/2009/08/27/the-trythis-method.aspx#comment-87</guid>
		<description>In most use-cases i would want to have more control about the type of exceptions that are swallowed... Probably something like:

private bool TryThis(Action block, Func&lt;Exception, bool&gt; exceptionHandler)
{
 try
 {
  block();
  return true;
 }
 catch(Exception ex)
 {
 if(!exceptionHandler(ex)) throw;
 return false;
 }
}

</description>
		<content:encoded><![CDATA[<p>In most use-cases i would want to have more control about the type of exceptions that are swallowed&#8230; Probably something like:</p>
<p>private bool TryThis(Action block, Func<exception , bool> exceptionHandler)<br />
{<br />
 try<br />
 {<br />
  block();<br />
  return true;<br />
 }<br />
 catch(Exception ex)<br />
 {<br />
 if(!exceptionHandler(ex)) throw;<br />
 return false;<br />
 }<br />
}</p>
<p></exception></p>
]]></content:encoded>
	</item>
</channel>
</rss>
