tags:

views:

50

answers:

2

Hello, in the following code

begin
 raise StandardError, 'message'
 #some code that raises a lot of exception
rescue StandardError
 #handle error
rescue OtherError
 #handle error
rescue YetAnotherError
 #handle error
end

I want to print a warning stating the type and the message of the error without adding print statement to each of the rescue clauses, like

begin
 raise StandardError, 'message'
 #some code that raises a lot of exception
rescue StandardError
 #handle error
rescue OtherError
 #handle error
rescue YetAnotherError
 #handle error
???
 print "An error of type #{???} happened, message is #{???}"
end
+3  A: 
begin
  raise ArgumentError, "I'm a description"
rescue Exception => ex
  puts "An error of type #{ex.class} happened, message is #{ex.message}"
end

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

sepp2k
And then if you still need specific handling for different types of errors, you can do that with a case..when.
cpm
A: 

You'd probably have to catch and re-throw

 begin
  # stuff
 rescue Exception => e
  puts "got #{e}"
  begin
     throw e # re -throw
  rescue ArgumentError
    #
  rescue Something
    #
  end
 end

or case statement it within the rescue, as noted, which would probably be better

rogerdpack