summaryrefslogtreecommitdiffhomepage
path: root/tests/custom/02_runtime/03_try_catch
blob: 751ca1dc5f5f208a17abcf127a40dbe75f399676 (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
Wrapping an exeptional operation in try {} catch {} allows handling the
resulting exception and to continue the execution flow.

-- Expect stdout --
Catched first exception.
Catched second exception: exception 2.
After exceptions.
-- End --

-- Testcase --
{%
	// A try-catch block that discards the exception information.
	try {
		die("exception 1");
	}
	catch {
		print("Catched first exception.\n");
	}

	// A try-catch block that captures the resulting exception in
	// the given variable.
	try {
		die("exception 2");
	}
	catch (e) {
		print("Catched second exception: ", e, ".\n");
	}

	print("After exceptions.\n");
%}
-- End --


Ensure that exceptions are propagated through C function calls.

-- Expect stderr --
exception
In [anonymous function](), line 3, byte 18:
  called from function replace ([C])
  called from anonymous function ([stdin]:4:3)

 `        die("exception");`
  Near here -------------^


-- End --

-- Testcase --
{%
	replace("test", "t", function(m) {
		die("exception");
	});
%}
-- End --


Ensure that exception can be catched through C function calls.

-- Expect stdout --
Caught exception: exception
-- End --

-- Testcase --
{%
	try {
		replace("test", "t", function(m) {
			die("exception");
		});
	}
	catch (e) {
		print("Caught exception: ", e, "\n");
	}
%}
-- End --


Ensure that exceptions are propagated through user function calls.

-- Expect stderr --
exception
In a(), line 3, byte 18:
  called from function b ([stdin]:7:5)
  called from function c ([stdin]:11:5)
  called from anonymous function ([stdin]:14:4)

 `        die("exception");`
  Near here -------------^


-- End --

-- Testcase --
{%
	function a() {
		die("exception");
	}

	function b() {
		a();
	}

	function c() {
		b();
	}

	c();
%}
-- End --


Ensure that exceptions can be caught in parent functions.

-- Expect stdout --
Caught exception: exception
-- End --

-- Testcase --
{%
	function a() {
		die("exception");
	}

	function b() {
		a();
	}

	function c() {
		try {
			b();
		}
		catch (e) {
			print("Caught exception: ", e, "\n");
		}
	}

	c();
%}
-- End --