Title photo
frugal technology, simple living and guerrilla large-appliance repair
Sun, 14 Jan 2018

Handling potential errors in Ruby with begin/rescue/else

I have a couple of points in my BlogPoster Ruby script where an error can cause the program to end, and I have been at odds as to how to deal with it.

I had been looking at the various Ruby Gems that I am using for tips on how they might handle errors more gracefully and didn't gain any insight.

Turns out I was looking in the wrong place.

Today I took a few minutes to search for how Ruby handles errors and exceptions, and I found the begin ... rescue ... else methods. The Bastards Book of Ruby explains them very well.

I quickly wrote this little program to make sure it worked (it does). Though I'm in the middle of a method-based rewrite of the BlogPoster program, I might hack this into the old script to see if I can keep the program from stopping when the Nokogiri and Twitter Gems encounter problems (the former with unresolvable URLs, the latter with connectivity issues).

Here is my "test" of `begin ... rescue ... else:

#!/usr/bin/env ruby

# Handling potential errors in Ruby with begin/rescue/else
# The 'error' here is trying to divide by zero.

begin
    1/0
rescue
    puts "The expression 1/0 doesn\'t work"
else
    puts "The expression 1/0 does work"
end

begin
    1/1
rescue
    puts "The expression 1/1 doesn\'t work"
else
    puts "The expression 1/1 does work"
end

# Expected output:
#
# The expression 1/0 doesn't work
# The expression 1/1 does work

Update: I tried this with "real" code, and it works!

Sat, 13 Jan 2018

ClojureScript syntax in 15 minutes | shaunlebron

ClojureScript syntax in 15 minutes | shaunlebron https://github.com/shaunlebron/ClojureScript-Syntax-in-15-minutes

Murdoch’s News Corp. warns Facebook to avoid ‘political’ changes | Bloomberg

Murdoch’s News Corp. warns Facebook to avoid ‘political’ changes | Bloomberg https://www.bloomberg.com/news/articles/2018-01-12/murdoch-s-news-corp-warns-facebook-to-avoid-political-changes

The state of Atom’s performance | blog.atom.io

The state of Atom’s performance | blog.atom.io http://blog.atom.io/2018/01/10/the-state-of-atoms-performance.html

2017 JavaScript rising stars (you have to do a little clicking once you get there)

2017 JavaScript rising stars (you have to do a little clicking once you get there) https://risingstars.js.org/2017/en/

Ruby still isn't dead | Engine Yard

Ruby still isn't dead | Engine Yard https://www.engineyard.com/blog/ruby-still-isnt-dead

Firefox bullshit removal via about:config | haasn

Firefox bullshit removal via about:config | haasn https://gist.github.com/haasn/69e19fc2fe0e25f3cff5

Modern JavaScript explained for dinosaurs | Medium

Modern JavaScript explained for dinosaurs | Medium https://medium.com/the-node-js-collection/modern-javascript-explained-for-dinosaurs-f695e9747b70

Unix's curl utility coming to Windows | daniel.haxx.se

Unix's curl utility coming to Windows | daniel.haxx.se https://daniel.haxx.se/blog/2018/01/13/microsoft-curls-too/

Fedora 27 on my HD-screen HP Envy laptop

I just booted into a Fedora 27 live system on my HP Envy laptop with an HD screen, and already the fonts in Firefox look better than stock Debian, which is to be expected.

Just like with Debian, I'm astounded that everything works in Linux out of the box. This laptop is about nine months old, and I have been avoiding running Linux for that whole time, choosing to explore Windows 10 (which is not bad at all, in case you were wondering).

I'm very happy that Fedora (which I ran for pretty much the entire "run" of my old HP Pavilion g6 laptop, which is running F27 as we speak) is so good on what, for me is new hardware.

The improvement in font rendering on this HD screen (1920x1080) is enough for me to say that I could definitely make the switch from Windows 10 to Fedora. I'm not ready just yet, but it looks like I am able.

Maybe it's the new laptop talking, but GNOME 3 looks more polished and usable than ever. The first thing I did in the live environment (after pumping up my Firefox magnification to 140%) was installing GNOME Tweak Tool and changing to the Adiwata Dark Theme.

True for both Debian and Fedora: The laptop is running very cool, too.

Next up: A test of the new non-BIOS-bricking Ubuntu 17.10, where I hope the GNOME 3 experience will also be a good one.