The Passionate Craftsman

Ruby, PHP, MySql, Software engineering and more.

Friday, 11 November 2011

Catch all exceptions in your Ruby program

Just put your code between begin and rescue statements and print the error. An example:

ree-1.8.7-head :010 > begin
ree-1.8.7-head :011 >
ree-1.8.7-head :012?> rescue Exception => msg
ree-1.8.7-head :013?> puts "Something went wrong ("+msg+")"
ree-1.8.7-head :014?> end
Something went wrong (uninitialized constant Testing)
=> nil

You can boot your application between begin and rescue and you will be able to print the error, perform an action, send an email in the rescue begin section:

rescue Exception => msg
puts "Something went wrong ("+msg+")"



Post a Comment

Subscribe to Post Comments [Atom]

<< Home