December 2, 2014

Ruby – Throw and Catch

In Ruby, there is an alternative to exceptions handling, and that is using the “throw/catch” system.


You can find all my latest posts on medium.

Throw/catch are specifically designed for getting out of nested loops.

For example, say of you are 4 loops deep, and you only want exit out to the second loop, then you can use throw/catch to do this.

Here’s an example:

catch :abort do 
  [1,2,3,4,5,6,7].each do |number|
    while number == 5
	  puts number
	  puts "need to abort infinite loop"
	  throw :abort 
	  puts number

The symbol “:abort” is used here in the sense of a label. It is used here to pair up the throw statement with the corresponding abort statement.

throw-catch even works across methods.

Throw/catch is just an alternative option you can use. But it’s unlikely that you will use it that heavily.