Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR ELIXIR

elixir try catch

defmodule SomeModule do
  @doc """
      iex> SomeModule.do_something()
      {:error, "error: %ArgumentError{message: "You provided a wrong argument"}"}
  """
  def do_something() do
    raise ArgumentError, "You provided a wrong argument"
  catch
    kind, error -> {:error, "#{kind}: #{inspect(error)}"}
  end

  @doc """
      iex> SomeModule.try_something()
      {:error, "error: %ArgumentError{message: "You provided a wrong argument"}"}
  """
  def try_something() do
    try do
      raise ArgumentError, "You provided a wrong argument"
    catch
      kind, error -> {:error, "#{kind}: #{inspect(error)}"}
    end
  end
end
Source by elixir-lang.org #
 
PREVIOUS NEXT
Tagged: #elixir #catch
ADD COMMENT
Topic
Name
3+9 =