<?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/"
	xmlns:series="http://unfoldingneurons.com/"
	>

<channel>
	<title>Man With Code &#187; handling</title>
	<atom:link href="http://manwithcode.com/tag/handling/feed/" rel="self" type="application/rss+xml" />
	<link>http://manwithcode.com</link>
	<description>Teaching You, One Tutorial at a Time</description>
	<lastBuildDate>Tue, 22 Feb 2011 23:40:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Programming With Ruby Episode 15, Error Handling</title>
		<link>http://manwithcode.com/211/programming-with-ruby-episode-15-error-handling/</link>
		<comments>http://manwithcode.com/211/programming-with-ruby-episode-15-error-handling/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 08:23:20 +0000</pubDate>
		<dc:creator>Tyler</dc:creator>
				<category><![CDATA[Ruby Programming]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[errors]]></category>
		<category><![CDATA[handling]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://manwithcode.com/?p=211</guid>
		<description><![CDATA[Covered in this Episode: What are errors What is error handling begin&#8230;end rescue ensure raise Transcript: Hello Everybody and welcome to Programming With Ruby Episode 15, Error Handling. I&#8217;m Tyler and this video is brought to you by manwithcode.com. In this episode you will learn what exactly errors are, and what error handling is. You [...]]]></description>
			<content:encoded><![CDATA[<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="425" height="344" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/97zxHTwEk6g&amp;hl=en&amp;fs=1" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="425" height="344" src="http://www.youtube.com/v/97zxHTwEk6g&amp;hl=en&amp;fs=1" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p><strong>Covered in this Episode:</strong></p>
<ul>
<li>What are errors</li>
<li>What is error handling</li>
<li>begin&#8230;end</li>
<li>rescue</li>
<li>ensure</li>
<li>raise</li>
</ul>
<p><strong>Transcript:</strong></p>
<p>Hello Everybody and welcome to Programming With Ruby Episode 15, Error<br />
Handling. I&#8217;m Tyler and this video is brought to you by<br />
manwithcode.com.</p>
<p>In this episode you will learn what exactly errors are, and what error<br />
handling is. You will also be learning how to use the begin&#8230;end,<br />
rescue, and ensure keywords, as well as the raise method.</p>
<p>Lets get started!</p>
<p><strong>What are Errors?</strong></p>
<p>There are two types of problems with programs. #1 is bugs, and #2 is<br />
errors. You&#8217;ve most likely heard of both of these types of problems<br />
before. But what are they?</p>
<p>Bugs are more subtle problems, the program still runs and acts<br />
normally, but it may be outputting the wrong data, or messing<br />
something else up. It is completely up to the programmer to find these<br />
bugs.</p>
<p>Errors actually stop the program from running, or at least running all<br />
the way through. There are ways of handling most types of errors,<br />
which you will be learning about in this episode.</p>
<p><strong>What is error handling?</strong></p>
<p>It is possible to catch some types of errors, which when left alone,<br />
would otherwise result in your program crashing.</p>
<p>Error handling is the process of catching those errors, and usually<br />
doing something about them, like informing the user, or executing on a<br />
contingency plan.</p>
<p>There are many things that can create errors, such as trying to read<br />
from a file that doesn&#8217;t exist, a user entering bad data, trying to<br />
call a method that does not exist, and the list goes on.</p>
<p>Some errors you can combat by using methods like File.exists? to check<br />
if a file exists before trying to open it. But there are other cases<br />
where this is not an option, or where you prefer to handle the problem<br />
a different way. In this episode I will show you how.</p>
<p><strong>Real Code</strong></p>
<p>Error handling starts with the begin&#8230;end block which looks like this:</p>
<pre class="brush: ruby; title: ; notranslate">
begin
    # Code here
end
</pre>
<p>In that block is where you put the code that has the possibility of<br />
generating an error. That block in itself doesn&#8217;t do anything, to<br />
actually handle the errors, you need to use the rescue keyword.</p>
<pre class="brush: ruby; title: ; notranslate">
begin
    # Possibly error inducing code here
rescue
    # What to do if an error happens
end
</pre>
<p>What ever is between &#8220;rescue&#8221; and &#8220;end&#8221; will be executed if an error<br />
occurs. But what if you have the potential for multiple errors? Then<br />
you must use multiple rescues specifying the error the handle:</p>
<pre class="brush: ruby; title: ; notranslate">
begin
    # Possibly error inducing code
rescue ArgumentError
    # If ArgumentError is raised
rescue NoMethodError
    # If NoMethodError is raised
end
</pre>
<p>What if you don&#8217;t know the exact name of the error? Either create the<br />
error yourself and look at the output when the program crashes. Go to<br />
<a title="Ruby Documentation" href="http://ruby-doc.org">http://ruby-doc.org</a> and find all classes ending Error. Or consult<br />
<a title="List of Errors" href="http://www.zenspider.com/Languages/Ruby/QuickRef.html#34">http://www.zenspider.com/Languages/Ruby/QuickRef.html#34</a></p>
<p>Now lets say you want something to happen regardless of whether or not<br />
the code generates an error. Say maybe you have to close a file, or<br />
end your connection to a database. Then you would want to use ensure!</p>
<pre class="brush: ruby; title: ; notranslate">
begin
    # ...
rescue
    # ...
ensure
    # This gets executed no matter what
end
</pre>
<p>Now that you know how to handle errors, how do you go about raising<br />
errors of your own? With raise, of course!</p>
<pre class="brush: ruby; title: ; notranslate">
def mymethod data
    if data.is_malformed?
        raise ArgumentError
    end
end
</pre>
<p>Why would you want to do this? It can serve as a better reminder to<br />
you or other programmers using your code that what they were doing is<br />
wrong. This can help stop bugs.</p>
<p>This brings us to the end of the episode.</p>
<p>If you have any questions, comments, or suggestions about anything<br />
related to Man With Code or these video tutorials; Please leave a<br />
comment below, or email me at tyler@manwithcode.com</p>
<p>Do not forget to donate. A lot of time and hard work went into making<br />
these. If you liked these videos the best way to show your<br />
appreciation is by donating.</p>
<p>Thanks for watching, goodbye!</p>
]]></content:encoded>
			<wfw:commentRss>http://manwithcode.com/211/programming-with-ruby-episode-15-error-handling/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<series:name><![CDATA[Ruby Programming]]></series:name>
	</item>
	</channel>
</rss>

