<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Nate Beck &#187; catch</title>
	<atom:link href="http://blog.natebeck.net/tag/catch/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.natebeck.net</link>
	<description>AIR, Flex / Flash, FMS, PushButton, Game... Developer</description>
	<lastBuildDate>Wed, 07 Sep 2011 21:24:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Tip of the Day &#8211; When to use try&#8230; catch&#8230; finally</title>
		<link>http://blog.natebeck.net/2009/01/tip-of-the-day-when-to-use-try-catch-finally/</link>
		<comments>http://blog.natebeck.net/2009/01/tip-of-the-day-when-to-use-try-catch-finally/#comments</comments>
		<pubDate>Sun, 18 Jan 2009 04:48:53 +0000</pubDate>
		<dc:creator>Nate Beck</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Tip of the day]]></category>
		<category><![CDATA[catch]]></category>
		<category><![CDATA[finally]]></category>
		<category><![CDATA[try]]></category>

		<guid isPermaLink="false">http://blog.natebeck.net/?p=275</guid>
		<description><![CDATA[A wise Adobean once told me to use try / catch / finally statements sparingly. It didn&#8217;t mean much to me at the time since I honestly don&#8217;t use them very often. Instead, I opt to check conditions prior to executing &#8220;at risk&#8221; statements. So&#8230; here&#8217;s my advice that when it comes to try /]]></description>
			<content:encoded><![CDATA[<p>A wise Adobean once told me to use try / catch / finally statements sparingly. It didn&#8217;t mean much to me at the time since I honestly don&#8217;t use them very often. Instead, I opt to check conditions prior to executing &#8220;at risk&#8221; statements.</p>
<p>So&#8230; here&#8217;s my advice that when it comes to try / catch statements.</p>
<h3>Preventing errors from occurring is far better than generally handling them.</h3>
<p>Take a look at this example.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> goodObj:<span style="color: #0066CC;">Object</span> = <span style="color: #66cc66;">&#123;</span>foo: <span style="color: #ff0000;">&quot;bar&quot;</span><span style="color: #66cc66;">&#125;</span>;
<span style="color: #000000; font-weight: bold;">var</span> badObj:<span style="color: #0066CC;">Object</span> = <span style="color: #000000; font-weight: bold;">null</span>;
&nbsp;
tryErrors<span style="color: #66cc66;">&#40;</span>goodObj<span style="color: #66cc66;">&#41;</span>;
tryErrors<span style="color: #66cc66;">&#40;</span>badObj<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> tryErrors<span style="color: #66cc66;">&#40;</span>obj:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">// DO THIS</span>
	<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>obj<span style="color: #66cc66;">&#41;</span>
		<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>obj.<span style="color: #006600;">foo</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #b1b100;">else</span>
		<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;doing something else&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">// NOT THIS</span>
	<span style="color: #0066CC;">try</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>obj.<span style="color: #006600;">foo</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span> <span style="color: #0066CC;">catch</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:TypeError<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;error occurred&quot;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*** OUTPUT ***************
bar
bar
doing something else
error occurred
***************************/</span></pre></div></div>

<h3>Do not use blanket try / catch statements.</h3>
<p>You gain nothing by catching an exception that your application can&#8217;t deal with at that point.<br />
For example, if you&#8217;re trying to convert a string to a number, and you know it might fail, but you have a default value, you should catch the exception, set the default value, and allow execution to continue.</p>
<p>Another example, if you try to call a Web Service and the call fails, and that particular call isn&#8217;t critical to your application, you could catch the exception, warn the user that some information is not available, and continue execution.</p>
<h3>Only use try / catch when you have to.</h3>
<p>I don&#8217;t have any specific benchmarks, but I do know that using excessive try/catch statements will degrade performance of your flash application. Probably because Flash Player is single threaded, but an Adobe person would probably have more information on that than I do.</p>
<p>An application I worked on a while ago used a try catch block around every single function&#8230; not my fault, it&#8217;s code that I inherited&#8230; anyways, it&#8217;s performance and execution times were horrendous.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.natebeck.net/2009/01/tip-of-the-day-when-to-use-try-catch-finally/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

