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