How to display error type in ruby?

If you want to show the original backtrace and highlighting, you can take advantage of Exception#full_message:

full_message(highlight: bool, order: [:top or :bottom]) → string

Returns formatted string of exception. The returned string is formatted using the same format that Ruby uses when printing an uncaught exceptions to stderr.

If highlight is true the default error handler will send the messages to a tty.

order must be either of :top or :bottom, and places the error message and the innermost backtrace come at the top or the bottom.

The default values of these options depend on $stderr and its tty? at the timing of a call.

begin
  raise ArgumentError, "I'm a description"
rescue StandardError => e
  puts e.full_message(highlight: true, order: :top)
end

begin
  raise ArgumentError, "I'm a description"
rescue => e
  puts "An error of type #{e.class} happened, message is #{e.message}"
end

Prints: An error of type ArgumentError happened, message is I'm a description

Tags:

Ruby

Exception