Skip to content

Exception Handling in Tim

mtaylor edited this page Apr 3, 2013 · 29 revisions

Exception/Error handling in Tim

Contributers please follow this guide for handling errors and exceptions in Tim.

Swallowing Exceptions

Swallowing exceptions is generally bad practice, however, there are times when swallowing an exception is a perfectly reasonable thing to do. These scenarios should be infrequent, if you are swallowing a lot of exceptions then it usually means you are not following this guide properly.

When is swallowing exceptions appropriate?

If you are creating a method that expects some exception (or an exception is a valid expectation) then you are safe to catch the exception and not log it (i.e. Swallow it). These scenarios should be infrequent. One example of an appropriate time to swallow could be something like as follows:

def can_connect_to_imagefactory?
  begin
    Tim::TargetImage.find(1)
  rescue ActiveResource::ConnectionError
    false
  rescue
    true
  end
  true
end

In the above method an ActiveResource::ConnectionError is expected when the server is unable to connect. There is no need in this case to log the error or raise it. When is swallowing an exception not appropriate?

When is swallowing exceptions not* appropriate?

Clone this wiki locally