Implicit try in Elixir

Article autor
September 9, 2025
Implicit try in Elixir
Elixir Newsletter
Join Elixir newsletter

Subscribe to receive Elixir news to your inbox every two weeks.

Oops! Something went wrong while submitting the form.
Elixir Newsletter
Expand your skills

Download free e-books, watch expert tech talks, and explore open-source projects. Everything you need to grow as a developer - completely free.

Table of contents

In the world of Elixir programming, there are numerous features and syntactic constructs that contribute to the language's elegance and expressiveness. One such hidden gem is the concept of "implicit try".

If you've been working with Elixir for a while, you might have encountered references to this concept, especially when dealing with errors and exception handling. In this exploration, we'll delve deeper into the implicit try mechanism, understand its purpose, and see how it can enhance the readability of your Elixir code.

Unveiling the Mystery

Imagine you're working on a project, and you stumble upon an intriguing error message from Credo, the widely-used static code analysis tool for Elixir. This message highlights the preference for using an implicit try instead of an explicit one, nudging you towards cleaner and more idiomatic code. Let's take a look at what this means in practice:

Code Readability

┃ [R] ↘ Prefer using an implicit `try` rather than explicit `try`.

The context is a function like the following:

defp value_to_integer(value) do
  try do
    String.to_integer(value)
  catch
    _ -> nil
  end
end

But what exactly is this implicit try concept? It turns out that this lesser-known feature was documented by none other than Jose Valim in 2016. In his contribution, he introduced a syntactic sugar that enhances the way error handling is expressed in Elixir code.

Demystifying Implicit Try

Implicit try is essentially a syntactic transformation that allows you to define a catch block after the function body, all without explicitly needing to use the try keyword. The primary motivation behind this feature is to improve code readability and reduce unnecessary verbosity. Here's a simplified example to illustrate how it works:

defp value_to_integer(value) do
  String.to_integer(value)
catch
  _ -> nil
end

By adopting this style, the code becomes more concise and flows naturally. It aligns with Elixir's philosophy of promoting clean and elegant code that is a joy to read and maintain.

Going Beyond the Basics

The benefits of implicit try don't stop at error handling. In fact, this style can be extended to other clauses such as rescue and after, enhancing the overall structure and clarity of your codebase. This further reinforces the idea that Elixir is not just a programming language but a platform for crafting beautiful and efficient software. Happy coding!

Related posts

Dive deeper into this topic with these related posts

No items found.

You might also like

Discover more content from this category

Treating warnings as errors in Elixir's mix compile

Warnings in Elixir are usually an important sign of a problem in the codebase. There is an easy way to make them gone.

How to safely handle related database operations with Ecto Multi

Sometimes you need to do some database operations at once. A simple example: User-A transfers money to User-B. Updating just one balance at the time creates a risk of data desynchronization. What if the first DB operation goes well but updating the second user’s data fails? Sounds like a hard to catch vulnerability.

Style spacing between repeated elements in CSS using flex gap

It's a pretty common scenario - you have to place a few elements in equal distances. E.g. unordered list items.