title: What is best strategy to handle exceptions & errors in Rails?
body: <p>I was wondering if people would share their best practices / strategies on handling exceptions &amp; errors. Now I'm not asking when to throw an exception ( it has been throroughly answered here:  <a href="https://stackoverflow.com/questions/77127/when-to-throw-an-exception">SO: When to throw an Exception</a>) . And I'm not using this for my application flow - but there are legitimate exceptions that happen all the time. For example the most popular one would be ActiveRecord::RecordNotFound. What would be the best way to handle it? The DRY way?</p>
  
  <p>Right now I'm doing a lot of checking within my controller so if <code>Post.find(5)</code> returns Nil - I check for that and throw a flash message. However while this is very granular - it's a bit cumbersome in a sense that I need to check for exceptions like that in every controller, while most of them are essentially the same and have to do with record not found or related records not found - such as either <code>Post.find(5)</code> not found or if you are trying to display comments related to post that doesn't exist, that would throw an exception (something like  <code>Post.find(5).comments[0].created_at</code>)  </p>
  
  <p>I know you can do something like this in ApplicationController and overwrite it later in a particular controller/method to get more granular support, however would that be a proper way to do it?</p>
  
  <pre><code>class ApplicationController &lt; ActionController::Base
      rescue_from ActiveRecord::RecordInvalid do |exception|
          render :action =&gt; (exception.record.new_record? ? :new : :edit)
      end
  end
  </code></pre>
  
  <p>Also this would work in case <code>Post.find(5)</code> not found, but what about <code>Post.find(5).comments[0].created_at</code> - I meant I can't throw a full blown exception if the post exists but has not comments, right?</p>
  
  <p>To summarize so far  I was doing a lot of manual checking using if/else/unless or case/when ( and I confess occasionally begin/rescue) and checking for nil? or empty?, etc. , but there's got to be a better way it seems. </p>
  
  <p><strong>REPLIES:</strong></p>
  
  <p>@Milan:
  Hi Milan 
  Thanks for a reply - I agree with what you said, and I think I misused the word exception. What I meant is that right now I do a lot of things like:</p>
  
  <pre><code>if Post.exists?(params[:post_id])
      @p = Post.find(params[:post_id])
  else
      flash[:error] = " Can't find Blog Post"
  end
  </code></pre>
  
  <p>And I do a lot of this kind of "exception handling", I try to avoid using begin/rescue. But it seems to me that this is a common enough result/verification/situation that there should be a DRYer way to do this, don't you? 
  How would you do this kind of check? </p>
  
  <p>Also how would handle it in this case?
  Let's say you want to display comment created date in your view:</p>
  
  <pre><code>Last comment for this post at : &lt;%= @post.comments[0].created_at %&gt;
  </code></pre>
  
  <p>And this post doesn't have any comments.
  You can do </p>
  
  <pre><code>Last comment for this post at : &lt;%= @post.comments.last.created_at unless @post.comments.empty? %&gt;
  </code></pre>
  
  <p>You could do a check in controller. Etc. There are several ways to do it. But what is the "best" way to handle this?</p>