exercism/elixir/stack-underflow/lib/rpn_calculator/exception.ex

26 lines
685 B
Elixir
Raw Normal View History

2024-03-07 07:05:57 +00:00
defmodule RPNCalculator.Exception do
defmodule DivisionByZeroError do
defexception message: "division by zero occurred"
end
defmodule StackUnderflowError do
defexception message: "stack underflow occurred"
@impl true
def exception(value) do
case value do
[] ->
%StackUnderflowError{}
_ ->
%StackUnderflowError{message: "stack underflow occurred, context: " <> value}
end
end
end
def divide([]), do: raise(StackUnderflowError, "when dividing")
def divide([_]), do: raise(StackUnderflowError, "when dividing")
def divide([0, _]), do: raise(DivisionByZeroError)
def divide([x, y]), do: div(y, x)
end