Wiki

root/cobra/trunk/Tests/100-basics/200-try-raise.cobra

Revision 1862, 2.7 KB (checked in by Chuck.Esterbrook, 3 years ago)

Fixed: A return statement in the try block of a try...success statement would circumvent the success block despite the fact that there was no failure (exception thrown).

  • Property svn:eol-style set to native
Line 
1namespace Test
2
3    class Test
4
5        def main
6            is shared
7
8            x as int = 0
9
10            try
11                throw Exception('')
12                x = 1
13            catch
14                pass
15            finally
16                x = 2
17            assert x==2
18
19            try
20                x = 1
21                throw Exception('')
22                x = 2
23            catch
24                pass
25            assert x==1
26
27
28            try
29                x = 0
30                throw Exception('')
31            catch e as Exception
32                x = 1
33            assert x==1
34
35
36            try
37                x = 0
38                throw Exception('')
39            catch nre as NullReferenceException
40                CobraCore.noOp(nre)
41                x = 1
42            catch
43                x = 2
44            assert x==2
45
46            try
47                x = 0
48                throw Exception('')
49            catch Exception  # no variable
50                x = 1
51            assert x==1
52
53            loc as int
54            count as int
55
56            # rethrow an exception
57            try
58                loc = 1
59                try
60                    loc = 2
61                    throw Exception('aoeu')
62                catch
63                    loc = 3
64                    throw
65            catch e as Exception
66                loc = 4
67                assert e.message=='aoeu'
68            assert loc==4
69
70            # test that the "success" block executes when no exception is throwd
71            try
72                loc = 1
73                count = 1
74            catch
75                loc = 2
76                count += 1
77            success
78                loc = 3
79                count += 1
80            assert loc==3
81            assert count==2
82
83
84            # test the the "catch" block does not execute when no exception is throwd
85            try
86                loc = 1
87                count = 1
88            catch
89                loc = 2
90                count += 1
91            success
92                loc = 3
93                count += 1
94            finally
95                loc = 4
96                count += 1
97            assert loc==4
98            assert count==3
99
100
101            # test that the "success" block does not execute when an exception is throwd
102            try
103                loc = 1
104                count = 1
105                throw Exception('')
106            catch
107                loc = 2
108                count += 1
109            success
110                loc = 3
111                count += 1
112            assert loc==2
113            assert count==2
114
115
116            # just for the heck of it
117            try
118                pass
119            catch
120                pass
121            success
122                pass
123            finally
124                pass
125
126            # this actually failed code gen at one point
127            try
128                pass
129            success
130                pass
131            finally
132                pass
133
134            # test that `success` and `finally` execute even with a `return` in the try part
135            .reset
136            .returnInTry
137            assert not _didCatch and _didSuccess and _didFinally
138
139            # test that `finally` executes even with an exception
140            .reset
141            expect Exception, .throwInSuccess
142            assert not _didCatch and _didSuccess and _didFinally
143
144        var _didCatch as bool is shared
145        var _didSuccess as bool is shared
146        var _didFinally as bool is shared
147       
148        def reset is shared
149            _didCatch = _didSuccess = _didFinally = false
150
151        def returnInTry is shared
152            try
153                return  # test that a return statement here doesn't side-step `success` or `finally`
154            catch
155                _didCatch = true
156                throw
157            success
158                _didSuccess = true
159            finally
160                _didFinally = true
161
162        def throwInSuccess is shared
163            try
164                pass
165            success
166                _didSuccess = true
167                throw Exception('aoeu'# test that an exception here doesn't side-step `finally`
168            finally
169                _didFinally = true
Note: See TracBrowser for help on using the browser.