Skip to content

Instantly share code, notes, and snippets.

@koolquark
Created June 21, 2019 16:04
Show Gist options
  • Save koolquark/bbc4c6557ca5552dc2fe95c1e0a2083e to your computer and use it in GitHub Desktop.
Save koolquark/bbc4c6557ca5552dc2fe95c1e0a2083e to your computer and use it in GitHub Desktop.
Elixir - try catch throw example
defmodule Excep do
def ex() do
try do
throw("a")
catch
c ->
IO.inspect c # "a"
end
end
end
Excep.ex()
defmodule Excep do
def ex() do
try do
1/0
catch
c ->
IO.inspect c # :badarith
end
end
end
Excep.ex()
defmodule Excep do
def ex() do
try do
1/0
catch
e,r ->
IO.inspect e # :error
IO.inspect r # :badarith
end
end
end
Excep.ex()
defmodule Excep do
def ex(v) do
try do
throw(v)
1/v
catch
e,r ->
IO.inspect e # :throw
IO.inspect r # 8
end
end
end
Excep.ex(8)
defmodule Excep do
def ex(v) do
try do
throw(v)
1/v
catch
c ->
IO.inspect c # 8
end
end
end
Excep.ex(8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment