Last Updated: February 25, 2016
·
499
· shime

Exceptional global variables

  • $! - contains the exception passed to the rescue block
  • $@ - contains the exception's caller or stack trace

Examples:

exception = 0/0 rescue $! # returns the exception 
                          # that would have been raised 

puts exception            # prints "divided by zero"
puts exception.message    # prints "divided by zero"
puts exception.class      # prints "ZeroDivisionError"
puts exception.backtrace  # prints the backtrace


trace = 0/0 rescue $@     # trace is an array
puts trace                # prints out the backtrace