Skip to content

Instantly share code, notes, and snippets.

@eprothro
Created September 26, 2022 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eprothro/aaed5778d998f536d3edc89a11704faf to your computer and use it in GitHub Desktop.
Save eprothro/aaed5778d998f536d3edc89a11704faf to your computer and use it in GitHub Desktop.
defmodule Test do
  def test() do
    raise_ex()
    IO.inspect "unreachable" 
  rescue 
    _ex -> IO.inspect("exception caught")
    :error
  end
  
  def test_with_ex() do
    with raise_ex() do
      IO.inspect "unreachable" 
    end
  rescue 
    _ex -> IO.inspect("exception caught")
    :error
  end
  
  def test_with_mismatch() do
    with :ok <- gen_failure() do
      IO.inspect "unreachable" 
    end
  rescue 
    _ex -> IO.inspect("exception caught")
    :error
  end
  
  defp raise_ex do
    raise "exception"
  end
  
  defp gen_failure() do
    :fail
  end
end
Test.test()
#=> "exception caught"
#=> :error
Test.test_with_ex()
#=> "exception caught"
#=> :error
Test.test_with_mismatch()
# => :fail
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment