<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/when-should-you-raise-an-exception-in-ruby--fyg31us5d" -->

---
title: When Should You Raise an Exception in Ruby? | daily.dev
description: An exploration of when to raise exceptions versus return values in Ruby, arguing exceptions should signal that an operation cannot fulfill its contract, not...
canonical: https://daily.dev/posts/when-should-you-raise-an-exception-in-ruby--fyg31us5d
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: When Should You Raise an Exception in Ruby? | daily.dev
og:description: An exploration of when to raise exceptions versus return values in Ruby, arguing exceptions should signal that an operation cannot fulfill its contract, not...
og:url: https://daily.dev/posts/when-should-you-raise-an-exception-in-ruby--fyg31us5d
og:image: https://api.daily.dev/og/posts/FYG31US5d.png
og:image:alt: When Should You Raise an Exception in Ruby?
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# When Should You Raise an Exception in Ruby?

**[RUBYLAND](https://daily.dev/sources/rubyla)** · 7 min read · 0 upvotes · 0 comments

## Summary

An exploration of when to raise exceptions versus return values in Ruby, arguing exceptions should signal that an operation cannot fulfill its contract, not represent ordinary branching. Contrasts Ruby's philosophy with Python's EAFP style, explains the performance and non-local control-flow costs of exceptions, and walks through paired Ruby/Rails APIs like hash#fetch vs hash[], Integer() with exception: false, User.find vs find_by, and save vs save! to illustrate how each choice communicates a different contract to callers.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://syedaslam.com/posts/when-should-you-raise-an-exception-in-ruby>

## Questions this post answers

### When should I use raise versus returning nil or false in Ruby?

Raise an exception when an operation cannot fulfill its contract and the caller should not continue normally; return a value when the outcome is part of the caller's ordinary decision tree. Frequency does not determine this - an invalid coupon code is expected even if rare, while a repeated database failure is still exceptional because it breaks the method's promise.

_daily.dev surfaces practical Ruby design writeups like this for developers refining their error-handling patterns._

### What is the difference between User.find and User.find_by in Rails?

User.find(id) raises ActiveRecord::RecordNotFound when no record matches, which Rails catches at the framework level and converts into a 404 response, making it convenient in controller actions. User.find_by(id: id) instead returns nil on a missing record, which is clearer when the caller needs to branch on absence as an expected, ordinary outcome rather than an exceptional one.

_Rails developers weighing find versus find_by can find pattern-focused breakdowns like this on daily.dev._

### Why are exceptions considered expensive compared to normal Ruby control flow like if or next?

Raising an exception requires creating an exception object, capturing stack trace information, and unwinding the call stack until Ruby locates a matching rescue clause, which is far more machinery than ordinary constructs like if, case, loops, break, or next. A loop wrapping raise/rescue around StopIteration on every iteration does substantially more work than simply calling next.

_daily.dev collects performance-minded language internals content for developers optimizing Ruby hot paths._

## Similar posts on daily.dev

- [Error handling in Elixir](https://daily.dev/posts/error-handling-in-elixir-sdezoyssq) · Honeybadger · 3 upvotes · 0 comments
- [What types of exceptions should you catch?](https://daily.dev/posts/what-types-of-exceptions-should-you-catch--jebkaugqp) · Planet Python · 1 upvotes · 0 comments
- [Ruby If Statement: A Complete Guide 2026](https://daily.dev/posts/ruby-if-statement-a-complete-guide-2026-rf0zoanbc) · Ruby Flow · 0 upvotes · 0 comments
- [Never Escape This Ruby Loop. Also, Here's How.](https://daily.dev/posts/never-escape-this-ruby-loop-also-here-s-how--4pbofog8d) · RUBYLAND · 0 upvotes · 0 comments

---

Tags: [#general-programming](https://daily.dev/tags/general-programming), [#python](https://daily.dev/tags/python), [#ruby](https://daily.dev/tags/ruby), [#rails](https://daily.dev/tags/rails)

[View this post on daily.dev](https://daily.dev/posts/when-should-you-raise-an-exception-in-ruby--fyg31us5d)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"When Should You Raise an Exception in Ruby?","url":"https://daily.dev/posts/when-should-you-raise-an-exception-in-ruby--fyg31us5d","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/when-should-you-raise-an-exception-in-ruby--fyg31us5d"},"datePublished":"2026-08-26T19:59:05.993Z","dateModified":"2026-08-26T19:59:30.012Z","description":"An exploration of when to raise exceptions versus return values in Ruby, arguing exceptions should signal that an operation cannot fulfill its contract, not...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/fab7016e96551db77e033fd7ecd3d689?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/fab7016e96551db77e033fd7ecd3d689?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"RUBYLAND","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"RUBYLAND","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/rubyla","url":"https://daily.dev/sources/rubyla"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/when-should-you-raise-an-exception-in-ruby--fyg31us5d","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"general-programming,python,ruby,rails","timeRequired":"PT7M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"RUBYLAND","item":"https://daily.dev/sources/rubyla"},{"@type":"ListItem","position":3,"name":"When Should You Raise an Exception in Ruby?"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/when-should-you-raise-an-exception-in-ruby--fyg31us5d#faq","mainEntity":[{"@type":"Question","name":"When should I use raise versus returning nil or false in Ruby?","acceptedAnswer":{"@type":"Answer","text":"Raise an exception when an operation cannot fulfill its contract and the caller should not continue normally; return a value when the outcome is part of the caller's ordinary decision tree. Frequency does not determine this - an invalid coupon code is expected even if rare, while a repeated database failure is still exceptional because it breaks the method's promise. daily.dev surfaces practical Ruby design writeups like this for developers refining their error-handling patterns."}},{"@type":"Question","name":"What is the difference between User.find and User.find_by in Rails?","acceptedAnswer":{"@type":"Answer","text":"User.find(id) raises ActiveRecord::RecordNotFound when no record matches, which Rails catches at the framework level and converts into a 404 response, making it convenient in controller actions. User.find_by(id: id) instead returns nil on a missing record, which is clearer when the caller needs to branch on absence as an expected, ordinary outcome rather than an exceptional one. Rails developers weighing find versus find_by can find pattern-focused breakdowns like this on daily.dev."}},{"@type":"Question","name":"Why are exceptions considered expensive compared to normal Ruby control flow like if or next?","acceptedAnswer":{"@type":"Answer","text":"Raising an exception requires creating an exception object, capturing stack trace information, and unwinding the call stack until Ruby locates a matching rescue clause, which is far more machinery than ordinary constructs like if, case, loops, break, or next. A loop wrapping raise/rescue around StopIteration on every iteration does substantially more work than simply calling next. daily.dev collects performance-minded language internals content for developers optimizing Ruby hot paths."}}]}
```

