Programming With Ruby Episode 15, Error Handling

Covered in this Episode:

  • What are errors
  • What is error handling
  • begin…end
  • rescue
  • ensure
  • raise

Transcript:

Hello Everybody and welcome to Programming With Ruby Episode 15, Error
Handling. I’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 will also be learning how to use the begin…end,
rescue, and ensure keywords, as well as the raise method.

Lets get started!

What are Errors?

There are two types of problems with programs. #1 is bugs, and #2 is
errors. You’ve most likely heard of both of these types of problems
before. But what are they?

Bugs are more subtle problems, the program still runs and acts
normally, but it may be outputting the wrong data, or messing
something else up. It is completely up to the programmer to find these
bugs.

Errors actually stop the program from running, or at least running all
the way through. There are ways of handling most types of errors,
which you will be learning about in this episode.

What is error handling?

It is possible to catch some types of errors, which when left alone,
would otherwise result in your program crashing.

Error handling is the process of catching those errors, and usually
doing something about them, like informing the user, or executing on a
contingency plan.

There are many things that can create errors, such as trying to read
from a file that doesn’t exist, a user entering bad data, trying to
call a method that does not exist, and the list goes on.

Some errors you can combat by using methods like File.exists? to check
if a file exists before trying to open it. But there are other cases
where this is not an option, or where you prefer to handle the problem
a different way. In this episode I will show you how.

Real Code

Error handling starts with the begin…end block which looks like this:

begin
    # Code here
end

In that block is where you put the code that has the possibility of
generating an error. That block in itself doesn’t do anything, to
actually handle the errors, you need to use the rescue keyword.

begin
    # Possibly error inducing code here
rescue
    # What to do if an error happens
end

What ever is between “rescue” and “end” will be executed if an error
occurs. But what if you have the potential for multiple errors? Then
you must use multiple rescues specifying the error the handle:

begin
    # Possibly error inducing code
rescue ArgumentError
    # If ArgumentError is raised
rescue NoMethodError
    # If NoMethodError is raised
end

What if you don’t know the exact name of the error? Either create the
error yourself and look at the output when the program crashes. Go to
http://ruby-doc.org and find all classes ending Error. Or consult
http://www.zenspider.com/Languages/Ruby/QuickRef.html#34

Now lets say you want something to happen regardless of whether or not
the code generates an error. Say maybe you have to close a file, or
end your connection to a database. Then you would want to use ensure!

begin
    # ...
rescue
    # ...
ensure
    # This gets executed no matter what
end

Now that you know how to handle errors, how do you go about raising
errors of your own? With raise, of course!

def mymethod data
    if data.is_malformed?
        raise ArgumentError
    end
end

Why would you want to do this? It can serve as a better reminder to
you or other programmers using your code that what they were doing is
wrong. This can help stop bugs.

This brings us to the end of the episode.

If you have any questions, comments, or suggestions about anything
related to Man With Code or these video tutorials; Please leave a
comment below, or email me at tyler@manwithcode.com

Do not forget to donate. A lot of time and hard work went into making
these. If you liked these videos the best way to show your
appreciation is by donating.

Thanks for watching, goodbye!