akalenuk
akalenuk

Reputation: 3845

How to throw an exception in Erlang?

I'm new to Erlang and I found how to handle exceptions in a Users Guide, but not how to throw them. Is it possible to define and then throw my own exception?

Upvotes: 9

Views: 7475

Answers (1)

PresleyDias
PresleyDias

Reputation: 3713

This is from raising Erlang exceptions.

Example of raising an Erlang exception with exit(Why).

-module(exceptions).

-export([sample_error/0]).

sample_error() -> throw(“some bad happened”).

Now lets compile our exceptions module, invoke the sample_error() function and observe >the output of the raised exception.

erlc –o ebin src/exceptions.erl
erl –pa ebin

1> exceptions:sample_error().
** exception throw: "some bad happened"
  in function  exceptions:sample_error/0

Upvotes: 4

Related Questions