-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathex.erl
66 lines (52 loc) · 1.3 KB
/
ex.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
-module(ex).
-export([th/0, safe/0, ctch_safe/0, ctch_throw/0, ctch_case/1, try_it/1, stacker/0, stackhelper/0, failing_case/1, safely/1, safely_failing_case/0, joker2/1]).
th() ->
throw({detta, fran, throw}).
safe() -> {'hejsan svejsan', 42}.
ctch_safe() ->
catch safe().
ctch_throw() ->
catch th().
ctch_case(N) ->
catch case N of
foo -> ok
end.
stacker() ->
A = 5,
B = stacker(A),
{A,B}.
stacker(N) ->
St = erlang:get_stacktrace(),
{"hello", St, N}.
stackhelper() -> {stacker(), 5}.
joker(N) ->
if N == 1 -> throw("denna strang kastas");
N == 2 -> erlang:exit("the exit term");
N == 3 -> erlang:error('the error term');
true -> {ok, N}
end.
joker2(N) ->
if N == 1 -> throw("denna strang kastas");
N == 2 -> erlang:exit("the exit term");
N == 3 -> erlang:error('the error term')
end.
try_it(N) ->
try joker(N) of
A -> A
catch
throw:Term -> {'thrown', 'term', Term};
exit:Reason -> {'exit', 'reason', Reason};
error:Reason -> {'error', 'reason', Reason}
end.
safely(F) ->
try F() of
R -> R
catch
error:Reason -> {"caught error with reason", Reason, erlang:get_stacktrace()}
end.
safely_failing_case() -> safely(fun() -> failing_case(wazaa) end).
failing_case(N) ->
case N of
ok -> {ok, N};
neok -> {neok, N}
end.