<p>Ruby has two different exceptions mechanisms: Throw/Catch and Raise/Rescue.</p>

<p>Why do we have two?</p>

<p>When should you use one and not the other?  </p>

[{'answer_id': 51037, 'author': 'readonly', 'author_id': 4883, 'author_profile': 'https://Stackoverflow.com/users/4883', 'pm_score': 8, 'selected': True, 'text': '<p>I think <a href="http://hasno.info/ruby-gotchas-and-caveats" rel="noreferrer">http://hasno.info/ruby-gotchas-and-caveats</a> has a decent explanation of the difference:</p>\n\n<blockquote>\n  <p>catch/throw are not the same as raise/rescue. catch/throw allows you to quickly exit blocks back to a point where a catch is defined for a specific symbol, raise rescue is the real exception handling stuff involving the Exception object.</p>\n</blockquote>\n'}, {'answer_id': 8692077, 'author': 'Jared Beck', 'author_id': 567762, 'author_profile': 'https://Stackoverflow.com/users/567762', 'pm_score': 7, 'selected': False, 'text': '<ul>\n<li><code>raise</code>, <code>fail</code>, <code>rescue</code>, and <code>ensure</code> handle <strong>errors</strong>, also known as <strong>exceptions</strong></li>\n<li><code>throw</code> and <code>catch</code> are <strong>control flow</strong></li>\n</ul>\n\n<blockquote>\n  <p>Unlike in other\n  languages, Ruby’s throw and catch are not used for exceptions.\n  Instead, they provide a way to terminate execution early when no\n  further work is needed.\n  (Grimm, 2011)</p>\n</blockquote>\n\n<p>Terminating a single level of control flow, like a <code>while</code> loop, can be done with a simple <code>return</code>.  Terminating many levels of control flow, like a nested loop, can be done with <code>throw</code>.</p>\n\n<blockquote>\n  <p>While the exception mechanism of raise and rescue is great for abandoning execution when things go wrong, it\'s sometimes nice to be able to jump out of some deeply nested construct during normal processing. This is where catch and throw come in handy. \n  (Thomas and Hunt, 2001)</p>\n</blockquote>\n\n<h3>References</h3>\n\n<ol>\n<li>Grimm, Avdi. "Throw, Catch, Raise, Rescue… I’m so Confused!" RubyLearning Blog. N.p., 11 July 2011. Web. 1 Jan. 2012. <a href="http://rubylearning.com/blog/2011/07/12/throw-catch-raise-rescue--im-so-confused/" rel="noreferrer">http://rubylearning.com/blog/2011/07/12/throw-catch-raise-rescue--im-so-confused/</a>. </li>\n<li>Thomas, Dave, and Andrew Hunt. "Programming Ruby." : The Pragmatic Programmer\'s Guide. N.p., 2001. Web. 29 Sept. 2015. <a href="http://ruby-doc.com/docs/ProgrammingRuby/html/tut_exceptions.html" rel="noreferrer">http://ruby-doc.com/docs/ProgrammingRuby/html/tut_exceptions.html</a>. </li>\n</ol>\n'}, {'answer_id': 27277044, 'author': 'Mark Amery', 'author_id': 1709587, 'author_profile': 'https://Stackoverflow.com/users/1709587', 'pm_score': 5, 'selected': False, 'text': '<p><a href="https://coderwall.com/p/lhkkug/don-t-confuse-ruby-s-throw-statement-with-raise" rel="noreferrer">https://coderwall.com/p/lhkkug/don-t-confuse-ruby-s-throw-statement-with-raise</a> offers an excellent explanation that I doubt I can improve on. To summarize, nicking some code samples from the blog post as I go:</p>\n\n<ol>\n<li><p><code>raise</code>/<code>rescue</code> are the closest analogues to the <code>throw</code>/<code>catch</code> construct you\'re familiar with from other languages (or to Python\'s <code>raise</code>/<code>except</code>). If you\'ve encountered an error condition and you would <code>throw</code> over it in another language, you should <code>raise</code> in Ruby.</p></li>\n<li><p>Ruby\'s <code>throw</code>/<code>catch</code> lets you break execution and climb up the stack looking for a <code>catch</code> (like <code>raise</code>/<code>rescue</code> does), but isn\'t really meant for error conditions. It should be used rarely, and is there just for when the "walk up the stack until you find a corresponding <code>catch</code>" behaviour makes sense for an algorithm you\'re writing but it wouldn\'t make sense to think of the <code>throw</code> as corresponding to an error condition.</p>\n\n<p><a href="https://stackoverflow.com/questions/3716801/what-is-catch-and-throw-used-for-in-ruby/">What is catch and throw used for in Ruby?</a> offers some suggestions on nice uses of the <code>throw</code>/<code>catch</code> construct.</p></li>\n</ol>\n\n<p>The concrete behavioural differences between them include:</p>\n\n<ul>\n<li><p><code>rescue Foo</code> will rescue instances of <code>Foo</code> including subclasses of <code>Foo</code>. <code>catch(foo)</code> will only catch <em>the same object, <code>Foo</code></em>. Not only can you not pass <code>catch</code> a class name to catch instances of it, but it won\'t even do equality comparisons. For instance</p>\n\n<pre><code>catch("foo") do\n  throw "foo"\nend\n</code></pre>\n\n<p>will give you an <code>UncaughtThrowError: uncaught throw "foo"</code> (or an <code>ArgumentError</code> in versions of Ruby prior to 2.2)</p></li>\n<li><p>Multiple rescue clauses can be listed...</p>\n\n<pre><code>begin\n  do_something_error_prone\nrescue AParticularKindOfError\n  # Insert heroism here.\nrescue\n  write_to_error_log\n  raise\nend\n</code></pre>\n\n<p>while multiple <code>catch</code>es need to be nested...</p>\n\n<pre><code>catch :foo do\n  catch :bar do\n    do_something_that_can_throw_foo_or_bar\n  end\nend\n</code></pre></li>\n<li><p>A bare <code>rescue</code> is equivalent to <code>rescue StandardError</code> and is an idiomatic construct. A "bare <code>catch</code>", like <code>catch() {throw :foo}</code>, will never catch anything and shouldn\'t be used.</p></li>\n</ul>\n'}]
['https://Stackoverflow.com/questions/51021', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2653/']