forked from clojerl/clojerl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtry.clje
89 lines (71 loc) · 1.68 KB
/
try.clje
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
(ns examples.try)
(def prn (fn* [x] (io/format "~s~n" #erl((clj_rt/str x)))))
(def assert (fn* [result] (if result nil (erlang/error :failed-assertion))))
(try (prn 1))
(try (erlang/+ 1 :a)
(catch :error error
(prn [:error error])))
(try (throw :hello)
(catch :throw error
(throw #erl[:not-catched error]))
(catch :exit error
(throw #erl[:not-catched error]))
(catch :error error
(prn [:throw error])))
(try (throw :hello-before-finally)
(catch :throw error
(throw #erl[:not-catched error]))
(catch :exit error
(throw #erl[:not-catched error]))
(catch :error error
(prn [:throw error]))
(finally
(prn :finally)))
;; Accept any error type with _
(try
(throw :throw)
(catch _ e
(prn e)))
(try
(erlang/throw :error)
(catch _ e
(prn e)))
(try
(erlang/exit :exit)
(catch _ e
(prn e)))
;; Use binding for error type
(try
(erlang/exit :foo)
(catch t e
(prn [t e])))
;; Use type name for error type
(try
(erlang/exit :foo)
(catch clojerl.ExceptionInfo e
(throw :bad-stuff))
(catch :exit e
(prn e)))
(try
(throw (clojerl.ExceptionInfo. "hello" {:some :info}))
(catch clojerl.ExceptionInfo e
(prn e))
(catch :error e
(throw :bad-stuff)))
;; Provide stacktrace
(try
(throw (clojerl.ExceptionInfo. "hello" {:some :info}) #erl())
(catch clojerl.ExceptionInfo e :stack st
(assert (erlang/== #erl() st))
(prn e))
(catch :error e
(throw :bad-stuff)))
;; [#684] Pattern matching a local
(let* [x 1
y (try
(throw 1)
(catch _ x :one)
(catch _ _ :not-one))]
(if (erlang/=:= y :one)
nil
(throw :error)))