# Ruby exception overhead

**URL:** <https://www.ruby-forum.com/t/ruby-exception-overhead/192764>\
**Category:** Ruby\
**Created:** [August 5, 2010, 5:57am UTC](https://www.ruby-forum.com/t/ruby-exception-overhead/192764 "2010-08-05T05:57:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dukeh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d6d6ee/32.png) [@dukeh](https://www.ruby-forum.com/u/dukeh)\
**Post date:** [August 5, 2010, 5:57am UTC](https://www.ruby-forum.com/t/ruby-exception-overhead/192764/1 "2010-08-05T05:57:57Z")

</div>

I’ve been seeing various bits and pieces mentioning the overhead Ruby’s  
(and other languages’) exception handling adds to a program (eg.  
comments on  
[http://rpheath.com/posts/237-raising-custom-exceptions-in-rails](http://rpheath.com/posts/237-raising-custom-exceptions-in-rails), and  
[How slow are (Ruby) Exceptions? - RubyFlow](http://www.rubyflow.com/items/3260)).

Is that overhead incurred even if the exception is not raised? That is;  
the negatives are only felt if the exception is raised, and so should  
not be used for normal program flow control, or is it better to avoid  
begin…rescue…end blocks wherever possible (if trying to improve your  
performance)?

Thanks.

---

<div class="post-metadata">

**Author:** ![dukeh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d6d6ee/32.png) [@dukeh](https://www.ruby-forum.com/u/dukeh)\
**Post date:** [August 5, 2010, 10:10am UTC](https://www.ruby-forum.com/t/ruby-exception-overhead/192764/2 "2010-08-05T10:10:05Z")

</div>

That’s the wrong approach to improving performance. Measure first, then  
change. In most cases the real bottleneck is not where you expected it  
to be.

If you have a begin…rescue…end block inside a _very_ tight loop  
executed thousands of times then maybe it would be worth removing it -  
but on the other hand, if you end up replacing it with more if… tests  
and method calls, then you could end up making it slower anyway.
