Photo by Neil Thomas on Unsplash

begin … rescue … end

Tracy Collins (aka threecee)
3 min readSep 28, 2020

--

Exception handling in Ruby and life

As a student in the Flatiron School’s web development program, we’ve learned to use Ruby on Rails for the backend. Until recently, we’ve not had to be concerned with exceptions.

Exception handling, where your code anticipates possible errors and “rescues” the application from crashing, is key to developing code that has to interface with the often predictable unpredictability of the real world.

One area where exception handling is required is when using APIs for web-based services such as Twitter, Google and countless others. Not all exceptions mean that something went wrong; they can also be used to signal temporary unavailability of a service (like during routine maintenance or discontinued endpoints) or changes to the API. Whether due to errors of the client, errors of the server, or some disconnect in transit, the user should not have to witness your app crashing & burning:

*** WATSON ERROR *** Image Analyzer error Error: , Code: , Information: {"images"=>[{"source_url"=>"http://localhost:3000/artwork/artists/threecee/images/technoMask.png", "resolved_url"=>"http://localhost:3000/artwork/artists/threecee/images/technoMask.png", "error"=>{"code"=>400, "description"=>"URL Fetcher error: Could not fetch URL: Disallowed Hostname specified", "error_id"=>"input_error"}}], "images_processed"=>1}, X-dp-watson-tran-id: 79774ff5-3c6a-4661-894b-e101551ce9bd, X-global-transaction-id: 79774ff5-3c6a-4661-894b-e101551ce9bd

For my current project, I’m using APIs from a number of arts institutions. The quality of their APIs varies widely, their servers aren’t as powerful as the big guy’s, and their documentation is often lacking or out of date. All of this requires that one be able to handle whatever their API may (or may not) throw at you without completely crashing your app. This is exception handling.

In Ruby, your code can “rescue” from an exception by wrapping the statements (in this case a call to an API) that potentially can throw an exception in ‘begin … rescue … end’:

begin
response = SomeApiThatWillThrowAnErrorSometimes(args)
doMoreStuffHere(response)
return
rescue StandardError => error
puts "Hey! Something went sideways: #{error.message}"
return error
end

If there are no errors during execution, the code between the begin and rescue will run as intended. However, if anything in those lines of code throws an error, the code in between rescue and end will run, and an error message will give clues as to what happened. Most importantly, the app won’t crash and die because of it.

There are many classes of exceptions in Ruby, giving you the ability to tailor a response particular to the situation. In the example above, I’ve used the StandardError class as it’s a general-purpose catch-all for most situations, and a good place to start if you’re not sure what errors may occur.

As I love a good metaphor, I see how exception handling applies to life. We all begin one day and start executing our lives. If we’re lucky, we have people who are looking out for us, ready to rescue us from exceptional circumstances, anticipated or not, so that we don’t crash and can continue running.

--

--