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.

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

How to redirect back to previous page in Elixir & Phoenix?

In this post, you'll learn how to easily redirect users to the previous path using the Navigation History plug.

How to override Kernel macros

The macro mechanism in Elixir is not only an interesting metaprogramming feature - in fact, it is at the language's very core. And the more awesome fact is that, using macros, you can override the algorithm of defining functions itself!

Load CSS as string using JS & Webpack import prefixes

People will tell you it's an antipattern, but what if a library needs you to do this?