<?xml version="1.0" encoding="utf-8"?><!-- generator="wordpress/1.5.1.3" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Don&#8217;t use exceptions for flow control</title>
	<link>http://www.sturmnet.org/blog/archives/2005/03/29/exceptions-flow/</link>
	<description>General musings and programming stuff</description>
	<pubDate>Tue, 06 Jan 2009 10:39:23 +0000</pubDate>
	<generator>http://wordpress.org/?v=1.5.1.3</generator>

	<item>
		<title>by: Oliver Sturm</title>
		<link>http://www.sturmnet.org/blog/archives/2005/03/29/exceptions-flow/#comment-3245</link>
		<pubDate>Thu, 29 Sep 2005 08:37:11 +0000</pubDate>
		<guid>http://www.sturmnet.org/blog/archives/2005/03/29/exceptions-flow/#comment-3245</guid>
					<description>Hi Ben - you are right, this is one of the unfortunate situations where the exception you want to avoid is not thrown from your own code, so it's not really in your hand to do anything about it. 

What would I suggest? Well, first of all: &quot;Don't use exceptions for flow control&quot; is a guideline, a goal you try to reach, not a low set in stone. So if there are situations where you find that using exceptions is actually the best, or the only, approach to a specific problem, you should probably just use them. In this case, I'd suggest use them, make a TODO comment in your code and replace that code with a call to TryParse when you change to .NET 2 at the earliest possible point.

Using regular expressions is an alternative, of course. Or even, for simple numeric values, to code up your own validity check - checking whether a string comprises of a number of numeric digits is not a hard task. When things are more difficult, the code-it-yourself approach is not so good, and I have never tested how the performance of regular expressions stacks up against catching an exception in a case like this. It's certainly a nicer way to implement the functionality, but then regular expressions are a big and mighty tool to call for when all you need is a simple numeric validity check.</description>
		<content:encoded><![CDATA[	<p>Hi Ben - you are right, this is one of the unfortunate situations where the exception you want to avoid is not thrown from your own code, so it&#8217;s not really in your hand to do anything about it. </p>
	<p>What would I suggest? Well, first of all: &#8220;Don&#8217;t use exceptions for flow control&#8221; is a guideline, a goal you try to reach, not a low set in stone. So if there are situations where you find that using exceptions is actually the best, or the only, approach to a specific problem, you should probably just use them. In this case, I&#8217;d suggest use them, make a TODO comment in your code and replace that code with a call to TryParse when you change to .NET 2 at the earliest possible point.</p>
	<p>Using regular expressions is an alternative, of course. Or even, for simple numeric values, to code up your own validity check - checking whether a string comprises of a number of numeric digits is not a hard task. When things are more difficult, the code-it-yourself approach is not so good, and I have never tested how the performance of regular expressions stacks up against catching an exception in a case like this. It&#8217;s certainly a nicer way to implement the functionality, but then regular expressions are a big and mighty tool to call for when all you need is a simple numeric validity check.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Ben Scheirman</title>
		<link>http://www.sturmnet.org/blog/archives/2005/03/29/exceptions-flow/#comment-3243</link>
		<pubDate>Wed, 28 Sep 2005 21:52:33 +0000</pubDate>
		<guid>http://www.sturmnet.org/blog/archives/2005/03/29/exceptions-flow/#comment-3243</guid>
					<description>I completely agree with you, but I have been &quot;dealing&quot; with it in one (quite common) scenario:

Int32.Parse().

In ASP.NET, when you request a querystring variable it comes back as a string.  A lot of times we expect an integer, but you obviously cannot trust it.  

int myIntVal = Int32.Parse( Request.QueryString[&quot;someVar&quot;] ); //this could throw an exception, in fact quite easily.  Whidbey brings us Int32.TryParse(), but for our 1.1 apps this doesn't help us.

What would you suggest?  Regular Expressions?</description>
		<content:encoded><![CDATA[	<p>I completely agree with you, but I have been &#8220;dealing&#8221; with it in one (quite common) scenario:</p>
	<p>Int32.Parse().</p>
	<p>In ASP.NET, when you request a querystring variable it comes back as a string.  A lot of times we expect an integer, but you obviously cannot trust it.  </p>
	<p>int myIntVal = Int32.Parse( Request.QueryString[&#8221;someVar&#8221;] ); //this could throw an exception, in fact quite easily.  Whidbey brings us Int32.TryParse(), but for our 1.1 apps this doesn&#8217;t help us.</p>
	<p>What would you suggest?  Regular Expressions?
</p>
]]></content:encoded>
				</item>
</channel>
</rss>
