Catching general exceptions in Ruby
Posted on August 18, 2006 - Filed Under Ruby
I was wondering how to catch exceptions of any kind in Ruby. Well You just use begin rescue pair, without specifying what kind of exceptions do You want to catch. This perfectly gets all exceptions. But when You want to use some information, like exception name written to user, You have to declare some variable. But what type? Well simplest solutions are not always obvious, at least for me ;-)
Use Exception class which is parent for all exception instances… Ughh been stuck on such easy thing is a shame :)
begin
# code
# code
rescue Exception => msg
puts "Something went wrong ("+msg+")"
end
Update
2008-01-10
There is much better way to do this, omit Exception in rescue and it will catch all exceptions
begin
# code
# code
resuce => msg
puts "Something went wrong ("+msg+")"
end
Popularity: 8% [?]
Hits for this post: 4712
Similar Posts
- Migrations headache
- $DEBUG variable in Ruby
- Quick fix for strange Ruby errors
- I was tempted
- Writing tests for Ruby/Tk application
Comments
2 Responses to “Catching general exceptions in Ruby”
Leave a Reply




That’s useful thanks
You should probably fix the spelling of ‘rescue’ though :-)
Oh, indeed :) Fixed ;)