Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR ELIXIR

elixir after

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

  @doc """
      iex> SomeModule.try_something()
      # Cleaning up...
      {:error, "%ArgumentError{message: "You provided a wrong argument"}"}
  """
  def try_something() do
    try do
      raise ArgumentError, "You provided a wrong argument"
    rescue
      error -> {:error, "#{inspect(error)}"}
    after
      clean_up()
    end
  end

  defp clean_up() do
    IO.puts("Cleaning up...")
  end
end
Source by elixir-lang.org #
 
PREVIOUS NEXT
Tagged: #elixir
ADD COMMENT
Topic
Name
6+5 =