Exception handling in Ruby

2020-12-16 hit count image

Let's see how to handle exceptions to prevent the program from terminating, when an unexpected error occurs in Ruby.


When we develop the programs, sometimes we get unexpected errors. When these exceptions occur, the programs are terminated unintentionally. The exception handling is to prevent the programs from terminating when we get errors like this situation.

Let’s see how to handle the exceptions in Ruby.

Exception handling

Other program languages normally use the try-catch statement. However, In Ruby, we use the begin-rescue-end statement to handle exceptions.

  # Where an exception might occur
  # Handling when exception occurs
  # Handling when no exception occurs
  # Last processing to be executed

As above, between begin and rescue, we write the code where unexpected errors may occur. If the errors occurs in here, the code between rescue and else is executed and lastly the code between ensure and `end is executed.

If the error doesn’t occur between begin and rescue, the code between else and ensure is executed and lastly the code between ensure and end is executed.

Debugging variables

When the error occurs and the exception handling is executed in Ruby, the following variables are automatically assigned.

  • $!₩: Information of the last raised exception 마지막으로 일어난 예외와 관련된 정보
  • $@: Location information of the last raised exception


You can use the exception handling like below.

puts "[Search a file with file name]"
  print "file name: "
  file_name = gets.chop
  puts File.ftype(file_name)
  puts "Error!!"
  puts "#{$@}"
  puts "#{$!}"

Let’s see the source code carefully.

file_name = gets.chop

The file name to be searched is received from the user through the gets method and use chop to remove newline characters(\n) from the input string.

puts File.ftype(file_name)

Using ftype method of File gets the file type from the input string.

[Search a file with file name]
file name: example.txt

If the file of the file name exists, the file type will be shown like above. If the file doesn’t exist, an error will occur.

  puts "Error!!"
  puts "#{$@}"
  puts "#{$!}"

If the error occurs, the code between rescue and end is executed and the Error!! string will be shown on the screen, and the location information of the error($@) and the error information($!) will be shown, too.

No such file or directory @ rb_file_s_ftype - test
["./index.rb:5:in `ftype'", "./index.rb:5:in `<main>'"]


We’ve seen how to handle the exception in Ruby. Now, you can use begin-rescue to prevent the program from terminating!

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

App promotion

You can use the applications that are created by this blog writer Deku.
Deku created the applications with Flutter.

If you have interested, please try to download them for free.