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
Written by Hrvoje Šimić
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#