Wednesday, July 18, 2007

Exception handling

I have seen a lot of code, that handle exception in the following way
begin
  do_some_things()
rescue
  #... print message, closing file/socket etc. 
end
There is a problem with this style. If there is error, we lost information where it happens because it is quite common that do_some_things() again call methods of others classes located in different files and so all.
I think that it should be a rule, that we alway print/save to logfile backtrace of exception in every exception handling code
begin
  do_some_things()
rescue => e
  require 'pp'
  pp e
  pp e.backtrace
  #... print message, closing file/socket etc. 
end
The backtrace information will help us to identify bug during development and to analyze root cause of exception during operation.

No comments: