Ruby Exception Handling

Ruby Exception Handling: TypeError

Next on the docket of our Ruby Exception Handling series is the lovable TypeError class. TypeErrors are fairly simple and are raised when an object that was passed as an argument is not of the expected type.

In this article we’ll look at TypeErrors a bit closer and see where it resides in the Ruby Exception class hierarchy. We’ll also dig into a few simple, functional code examples to see how TypeErrors are commonly raised in the hopes of helping you during your own coding endeavors, so let’s get crackin’!

The Technical Rundown

  • All Ruby exceptions are descendants of the Exception class, or a subclass therein.
  • StandardError is a direct descendant of the Exception class, and is also a superclass with many descendants of its own.
  • TypeError is the direct descendant of StandardError.

When Should You Use It?

Raising a TypeError is a rather simple affair — it commonly occurs when trying to pass an incorrect data type to a method. Many API functions and methods expect a specific type of object to be passed as arguments, so any attempts to pass invalid types usually results in a failure, and sometimes even the raising of a TypeError depending how that module was written.

To see TypeErrors in action let’s start with some example code. Below we have the full working code example, after which we’ll break it down into easier chunks to see what’s going on:

To start we have our array_example method where we’ve listed a few book titles in an array and we want to #sort that array alphabetically and then grab the first four elements using the #first method:

As expected this works just fine and outputs the first four alphabetized titles:

However, now in our invalid_array_example method we’re going to try something much the same, but we’re accidentally passing the String value of '4' instead of the Integer value of 4 to the #first method of our array:

As it turns, if we look directly at the source code of Ruby’s #first method we see that it requires an integer (int) parameter for the first value (argc):

Therefore, our above Ruby code raises a TypeError for our troubles, informing us that we cannot implicitly convert our String value to an Integer that is required:

As it happens there are near-countless methods in Ruby that will raise TypeErrors if given improper argument data types. Rather than an array let’s try a String using the #insert method, which expects an Integer as the first argument to indicate the index of where the second argument should be inserted into the targetted string. Here we’re adding the missing word 'The' to our title then outputting the result:

That works just fine, but once again if we pass a non-integer value the #insert method doesn’t know what to do. The invalid_string_example method throws another TypeError just as we saw before:

To get the most out of your own applications and to fully manage any and all Ruby Exceptions, check out the Airbrake Ruby exception handling tool, offering real-time alerts and instantaneous insight into what went wrong with your Ruby code, including integrated support for a variety of popular Ruby gems and frameworks.