<?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"
	>
<channel>
	<title>Comments on: Your Mom is null throws Exception OT OT OT &#8230; or Learning Exception Handling in ActionScript</title>
	<atom:link href="http://jessewarden.com/2007/11/your-mom-is-null-throws-exception-ot-ot-ot-or-learning-exception-handling-in-actionscript.html/feed" rel="self" type="application/rss+xml" />
	<link>http://jessewarden.com/2007/11/your-mom-is-null-throws-exception-ot-ot-ot-or-learning-exception-handling-in-actionscript.html</link>
	<description>A blog on software development, technology, games &#038; movies.</description>
	<pubDate>Wed, 09 Jul 2008 04:36:46 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5</generator>
		<item>
		<title>By: Peter Hall</title>
		<link>http://jessewarden.com/2007/11/your-mom-is-null-throws-exception-ot-ot-ot-or-learning-exception-handling-in-actionscript.html#comment-28618</link>
		<dc:creator>Peter Hall</dc:creator>
		<pubDate>Sat, 24 Nov 2007 16:34:33 +0000</pubDate>
		<guid isPermaLink="false">http://jessewarden.com/2007/11/your-mom-is-null-throws-exception-ot-ot-ot-or-learning-exception-handling-in-actionscript.html#comment-28618</guid>
		<description>One solution for the future might be the "nullable types" proposal for ES4. (http://wiki.ecmascript.org/doku.php?id=proposals:nullability). Not much help today of course...</description>
		<content:encoded><![CDATA[<p>One solution for the future might be the &#8220;nullable types&#8221; proposal for ES4. (http://wiki.ecmascript.org/doku.php?id=proposals:nullability). Not much help today of course&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JesterXL</title>
		<link>http://jessewarden.com/2007/11/your-mom-is-null-throws-exception-ot-ot-ot-or-learning-exception-handling-in-actionscript.html#comment-28458</link>
		<dc:creator>JesterXL</dc:creator>
		<pubDate>Sat, 24 Nov 2007 02:44:48 +0000</pubDate>
		<guid isPermaLink="false">http://jessewarden.com/2007/11/your-mom-is-null-throws-exception-ot-ot-ot-or-learning-exception-handling-in-actionscript.html#comment-28458</guid>
		<description>After reading that, I'm inclined to:

- try/catch to capture all errors inside my class(es)
- never use throw again.  Instead, dispatch an error event that abstracts all errors into one event.
- have that error event include the reason why something broke

If you code ActionScript, you understand events.  Events are optional, just like RuntimeExceptions (unchecked exceptions) in Java.  So... seems good enough for me!  Heck, even &lt;a href="http://livedocs.adobe.com/flex/201/langref/mx/rpc/http/HTTPService.html" rel="nofollow"&gt;HTTPService&lt;/a&gt; does this via the fault event.  No exceptions there... good enough for Flex, good enough for me.</description>
		<content:encoded><![CDATA[<p>After reading that, I&#8217;m inclined to:</p>
<p>- try/catch to capture all errors inside my class(es)<br />
- never use throw again.  Instead, dispatch an error event that abstracts all errors into one event.<br />
- have that error event include the reason why something broke</p>
<p>If you code ActionScript, you understand events.  Events are optional, just like RuntimeExceptions (unchecked exceptions) in Java.  So&#8230; seems good enough for me!  Heck, even <a href="http://livedocs.adobe.com/flex/201/langref/mx/rpc/http/HTTPService.html" rel="nofollow">HTTPService</a> does this via the fault event.  No exceptions there&#8230; good enough for Flex, good enough for me.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Deitte</title>
		<link>http://jessewarden.com/2007/11/your-mom-is-null-throws-exception-ot-ot-ot-or-learning-exception-handling-in-actionscript.html#comment-28453</link>
		<dc:creator>Brian Deitte</dc:creator>
		<pubDate>Sat, 24 Nov 2007 02:01:16 +0000</pubDate>
		<guid isPermaLink="false">http://jessewarden.com/2007/11/your-mom-is-null-throws-exception-ot-ot-ot-or-learning-exception-handling-in-actionscript.html#comment-28453</guid>
		<description>Hey Jesse, interesting post.  Checked exceptions is still something that people aren't sure about in Java. The fact that C# chose not to have them brought a lot of discussion a few years ago.  There's a lot of good articles on this: see http://www.mindview.net/Etc/Discussions/CheckedExceptions

The strangest thing to me with exceptions is something small, which is that binding swallows a few exception (see BindingUtil).  That's caught me a few times.</description>
		<content:encoded><![CDATA[<p>Hey Jesse, interesting post.  Checked exceptions is still something that people aren&#8217;t sure about in Java. The fact that C# chose not to have them brought a lot of discussion a few years ago.  There&#8217;s a lot of good articles on this: see <a href="http://www.mindview.net/Etc/Discussions/CheckedExceptions" rel="nofollow">http://www.mindview.net/Etc/Discussions/CheckedExceptions</a></p>
<p>The strangest thing to me with exceptions is something small, which is that binding swallows a few exception (see BindingUtil).  That&#8217;s caught me a few times.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JesterXL</title>
		<link>http://jessewarden.com/2007/11/your-mom-is-null-throws-exception-ot-ot-ot-or-learning-exception-handling-in-actionscript.html#comment-28431</link>
		<dc:creator>JesterXL</dc:creator>
		<pubDate>Fri, 23 Nov 2007 22:05:14 +0000</pubDate>
		<guid isPermaLink="false">http://jessewarden.com/2007/11/your-mom-is-null-throws-exception-ot-ot-ot-or-learning-exception-handling-in-actionscript.html#comment-28431</guid>
		<description>For now, throw an exception if I can’t recover. For some methods, it’s ok if you pass a null value. However, for UUID’s, for example, the represent a massive amount of data that I need from the server… no UUID, no data.

Rather than returning false, or trying the call anyway (knowing it’s going to fail), I clean up the mess (remove event listeners from the URLLoader, delete it) and then throw an exception. The exception for typical messages is just an error:

&lt;pre&gt;&lt;code&gt;throw new Error ( “Cannot pass a null value for a UUID.”);&lt;/code&gt;&lt;/pre&gt;

For some of the API stuff I’m doing, however, I feel like, based on what I read from the Java articles, encapsulate it into a custom event. I do the same as the above for the custom error:

&lt;pre&gt;&lt;code&gt;throw new APIError ( “Cannot pass a null value for a UUID.”);&lt;/code&gt;&lt;/pre&gt;

For internal things, like bad data, errors returned from the database because it’s down, or just no internet connection, I’ll capture all 3 and put into a generic error:

&lt;pre&gt;&lt;code&gt;var err:APIError = new APIError ( “Cannot retrieve data from the server.”);
err.rootCause = theIOError;
throw err;&lt;/code&gt;&lt;/pre&gt;

That way, people using the API only have to do:

&lt;pre&gt;&lt;code&gt;try
{
   api.getPeople ( “3xedrg5″ );
}
catch ( err : APIError )
{
   showErrorScreen();
}&lt;/code&gt;&lt;/pre&gt;

This includes asyncronous ones:

&lt;pre&gt;&lt;code&gt;api.addEventListener ( APIErrorEvent.ERROR, onAPIError );
function onAPIError ()
{
   showErrorScreen();
}&lt;/code&gt;&lt;/pre&gt;

The generic ones help in debugging.

However, key thing to note, if I know that a simple operation will fail, say passing NaN to an addChild method, I wouldn’t catch that myself. The crackhead who passed the NaN should either inspect his/her own data, and/or try/catch for RangeError’s. The downside is, unlike Java, he has no clue he’ll even GET a RangeError because there is no “throws” like thing in Java. Part of me wants to encapsulate those as well; store the root cause in my custom error, and just throw an APIError.

The other part of me doesn’t want to allow bad data in, and then hide the true cause 1 level away. If you are passing in NaN’s, that’s the API user’s fault.  I’m leaning more towards the custom exception, though, because I feel I can write better error messages than what the defaults imply. So, instead of:

&lt;pre&gt;&lt;code&gt;var a:MovieClip = new MovieClip();
addChildAt(a, 20);&lt;/code&gt;&lt;/pre&gt;

Causing:

&lt;pre&gt;&lt;code&gt;RangeError: Error #2006: The supplied index is out of bounds.&lt;/code&gt;&lt;/pre&gt;

In the API, it’d instead attempt to find a valid depth, and then throw:

&lt;pre&gt;&lt;code&gt;APIError: “Cannot add a child to a depth of NaN. Put to depth 0 instead.”&lt;/code&gt;&lt;/pre&gt;

The root cause is in there, and they can print out the stack trace if they so choose. Furthermore, the error gives more context.

:: shrugs :: I’m new at this, no hard and fast opinons yet.</description>
		<content:encoded><![CDATA[<p>For now, throw an exception if I can’t recover. For some methods, it’s ok if you pass a null value. However, for UUID’s, for example, the represent a massive amount of data that I need from the server… no UUID, no data.</p>
<p>Rather than returning false, or trying the call anyway (knowing it’s going to fail), I clean up the mess (remove event listeners from the URLLoader, delete it) and then throw an exception. The exception for typical messages is just an error:</p>
<pre><code>throw new Error ( “Cannot pass a null value for a UUID.”);</code></pre>
<p>For some of the API stuff I’m doing, however, I feel like, based on what I read from the Java articles, encapsulate it into a custom event. I do the same as the above for the custom error:</p>
<pre><code>throw new APIError ( “Cannot pass a null value for a UUID.”);</code></pre>
<p>For internal things, like bad data, errors returned from the database because it’s down, or just no internet connection, I’ll capture all 3 and put into a generic error:</p>
<pre><code>var err:APIError = new APIError ( “Cannot retrieve data from the server.”);
err.rootCause = theIOError;
throw err;</code></pre>
<p>That way, people using the API only have to do:</p>
<pre><code>try
{
   api.getPeople ( “3xedrg5″ );
}
catch ( err : APIError )
{
   showErrorScreen();
}</code></pre>
<p>This includes asyncronous ones:</p>
<pre><code>api.addEventListener ( APIErrorEvent.ERROR, onAPIError );
function onAPIError ()
{
   showErrorScreen();
}</code></pre>
<p>The generic ones help in debugging.</p>
<p>However, key thing to note, if I know that a simple operation will fail, say passing NaN to an addChild method, I wouldn’t catch that myself. The crackhead who passed the NaN should either inspect his/her own data, and/or try/catch for RangeError’s. The downside is, unlike Java, he has no clue he’ll even GET a RangeError because there is no “throws” like thing in Java. Part of me wants to encapsulate those as well; store the root cause in my custom error, and just throw an APIError.</p>
<p>The other part of me doesn’t want to allow bad data in, and then hide the true cause 1 level away. If you are passing in NaN’s, that’s the API user’s fault.  I’m leaning more towards the custom exception, though, because I feel I can write better error messages than what the defaults imply. So, instead of:</p>
<pre><code>var a:MovieClip = new MovieClip();
addChildAt(a, 20);</code></pre>
<p>Causing:</p>
<pre><code>RangeError: Error #2006: The supplied index is out of bounds.</code></pre>
<p>In the API, it’d instead attempt to find a valid depth, and then throw:</p>
<pre><code>APIError: “Cannot add a child to a depth of NaN. Put to depth 0 instead.”</code></pre>
<p>The root cause is in there, and they can print out the stack trace if they so choose. Furthermore, the error gives more context.</p>
<p>:: shrugs :: I’m new at this, no hard and fast opinons yet.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David B.</title>
		<link>http://jessewarden.com/2007/11/your-mom-is-null-throws-exception-ot-ot-ot-or-learning-exception-handling-in-actionscript.html#comment-28418</link>
		<dc:creator>David B.</dc:creator>
		<pubDate>Fri, 23 Nov 2007 21:23:22 +0000</pubDate>
		<guid isPermaLink="false">http://jessewarden.com/2007/11/your-mom-is-null-throws-exception-ot-ot-ot-or-learning-exception-handling-in-actionscript.html#comment-28418</guid>
		<description>I've also been struggling with this dilemma lately. So, do your methods still check for null and NaN when they accept parameters with no default values? If they do, what do you do when you do get a null or NaN? Do you return an error code, throw an exception, do nothing, etc? And do you do this for each and every method or only those that are public?</description>
		<content:encoded><![CDATA[<p>I&#8217;ve also been struggling with this dilemma lately. So, do your methods still check for null and NaN when they accept parameters with no default values? If they do, what do you do when you do get a null or NaN? Do you return an error code, throw an exception, do nothing, etc? And do you do this for each and every method or only those that are public?</p>
]]></content:encoded>
	</item>
</channel>
</rss>
