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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
|
" Test try-catch-finally exception handling
" Most of this was formerly in test49.
source check.vim
source shared.vim
"-------------------------------------------------------------------------------
" Test environment {{{1
"-------------------------------------------------------------------------------
com! XpathINIT let g:Xpath = ''
com! -nargs=1 -bar Xpath let g:Xpath = g:Xpath . <args>
" Test 25: Executing :finally clauses on normal control flow {{{1
"
" Control flow in a :try conditional should always fall through to its
" :finally clause. A :finally clause of a :try conditional inside an
" inactive conditional should never be executed.
"-------------------------------------------------------------------------------
func T25_F()
let loops = 3
while loops > 0
Xpath 'a' . loops
if loops >= 2
try
Xpath 'b' . loops
if loops == 2
try
Xpath 'c' . loops
finally
Xpath 'd' . loops
endtry
endif
finally
Xpath 'e' . loops
if loops == 2
try
Xpath 'f' . loops
final
Xpath 'g' . loops
endtry
endif
endtry
endif
Xpath 'h' . loops
let loops = loops - 1
endwhile
Xpath 'i'
endfunc
" Also try using "fina" and "final" and "finall" as abbreviations.
func T25_G()
if 1
try
Xpath 'A'
call T25_F()
Xpath 'B'
fina
Xpath 'C'
endtry
else
try
Xpath 'D'
finall
Xpath 'E'
endtry
endif
endfunc
func Test_finally()
XpathINIT
call T25_G()
call assert_equal('Aa3b3e3h3a2b2c2d2e2f2g2h2a1h1iBC', g:Xpath)
endfunc
"-------------------------------------------------------------------------------
" Test 26: Executing :finally clauses after :continue or :break {{{1
"
" For a :continue or :break dynamically enclosed in a :try/:endtry
" region inside the next surrounding :while/:endwhile, if the
" :continue/:break is before the :finally, the :finally clause is
" executed first. If the :continue/:break is after the :finally, the
" :finally clause is broken (like an :if/:endif region).
"-------------------------------------------------------------------------------
func T26_F()
try
let loops = 3
while loops > 0
try
try
if loops == 2
Xpath 'a' . loops
let loops = loops - 1
continue
elseif loops == 1
Xpath 'b' . loops
break
finish
endif
Xpath 'c' . loops
endtry
finally
Xpath 'd' . loops
endtry
Xpath 'e' . loops
let loops = loops - 1
endwhile
Xpath 'f'
finally
Xpath 'g'
let loops = 3
while loops > 0
try
finally
try
if loops == 2
Xpath 'h' . loops
let loops = loops - 1
continue
elseif loops == 1
Xpath 'i' . loops
break
finish
endif
endtry
Xpath 'j' . loops
endtry
Xpath 'k' . loops
let loops = loops - 1
endwhile
Xpath 'l'
endtry
Xpath 'm'
endfunc
func Test_finally_after_continue()
XpathINIT
call T26_F()
call assert_equal('c3d3e3a2d1b1d1fgj3k3h2i1lm', g:Xpath)
endfunc
"-------------------------------------------------------------------------------
" Test 32: Remembering the :return value on :finally {{{1
"
" If a :finally clause is executed due to a :return specifying
" a value, this is the value visible to the caller if not overwritten
" by a new :return in the :finally clause. A :return without a value
" in the :finally clause overwrites with value 0.
"-------------------------------------------------------------------------------
func T32_F()
try
Xpath 'a'
try
Xpath 'b'
return "ABCD"
Xpath 'c'
finally
Xpath 'd'
endtry
Xpath 'e'
finally
Xpath 'f'
endtry
Xpath 'g'
endfunc
func T32_G()
try
Xpath 'h'
return 8
Xpath 'i'
finally
Xpath 'j'
return 16 + strlen(T32_F())
Xpath 'k'
endtry
Xpath 'l'
endfunc
func T32_H()
try
Xpath 'm'
return 32
Xpath 'n'
finally
Xpath 'o'
return
Xpath 'p'
endtry
Xpath 'q'
endfunc
func T32_I()
try
Xpath 'r'
finally
Xpath 's'
return T32_G() + T32_H() + 64
Xpath 't'
endtry
Xpath 'u'
endfunc
func Test_finally_return()
XpathINIT
call assert_equal(84, T32_I())
call assert_equal('rshjabdfmo', g:Xpath)
endfunc
"-------------------------------------------------------------------------------
" Test 33: :return under :execute or user command and :finally {{{1
"
" A :return command may be executed under an ":execute" or from
" a user command. Executing of :finally clauses and passing through
" the return code works also then.
"-------------------------------------------------------------------------------
func T33_F()
try
RETURN 10
Xpath 'a'
finally
Xpath 'b'
endtry
Xpath 'c'
endfunc
func T33_G()
try
RETURN 20
Xpath 'd'
finally
Xpath 'e'
RETURN 30
Xpath 'f'
endtry
Xpath 'g'
endfunc
func T33_H()
try
execute "try | return 40 | finally | return 50 | endtry"
Xpath 'h'
finally
Xpath 'i'
endtry
Xpath 'j'
endfunc
func T33_I()
try
execute "try | return 60 | finally | return 70 | endtry"
Xpath 'k'
finally
Xpath 'l'
execute "try | return 80 | finally | return 90 | endtry"
Xpath 'm'
endtry
Xpath 'n'
endfunc
func T33_J()
try
RETURN 100
Xpath 'o'
finally
Xpath 'p'
return
Xpath 'q'
endtry
Xpath 'r'
endfunc
func T33_K()
try
execute "try | return 110 | finally | return 120 | endtry"
Xpath 's'
finally
Xpath 't'
execute "try | return 130 | finally | return | endtry"
Xpath 'u'
endtry
Xpath 'v'
endfunc
func T33_L()
try
return
Xpath 'w'
finally
Xpath 'x'
RETURN 140
Xpath 'y'
endtry
Xpath 'z'
endfunc
func T33_M()
try
return
Xpath 'A'
finally
Xpath 'B'
execute "try | return 150 | finally | return 160 | endtry"
Xpath 'C'
endtry
Xpath 'D'
endfunc
func T33_N()
RETURN 170
endfunc
func T33_O()
execute "try | return 180 | finally | return 190 | endtry"
endfunc
func Test_finally_cmd_return()
command! -nargs=? RETURN
\ try | return <args> | finally | return <args> * 2 | endtry
XpathINIT
call assert_equal(20, T33_F())
call assert_equal(60, T33_G())
call assert_equal(50, T33_H())
call assert_equal(90, T33_I())
call assert_equal(0, T33_J())
call assert_equal(0, T33_K())
call assert_equal(280, T33_L())
call assert_equal(160, T33_M())
call assert_equal(340, T33_N())
call assert_equal(190, T33_O())
call assert_equal('beilptxB', g:Xpath)
delcommand RETURN
endfunc
"-------------------------------------------------------------------------------
" Test 41: Skipped :throw finding next command {{{1
"
" A :throw in an inactive conditional must not hide a following
" command.
"-------------------------------------------------------------------------------
func T41_F()
Xpath 'a'
if 0 | throw 'never' | endif | Xpath 'b'
Xpath 'c'
endfunc
func T41_G()
Xpath 'd'
while 0 | throw 'never' | endwhile | Xpath 'e'
Xpath 'f'
endfunc
func T41_H()
Xpath 'g'
if 0 | try | throw 'never' | endtry | endif | Xpath 'h'
Xpath 'i'
endfunc
func Test_throw_inactive_cond()
XpathINIT
try
Xpath 'j'
call T41_F()
Xpath 'k'
catch /.*/
Xpath 'l'
call assert_report('Caught ' . v:exception . ' in ' . v:throwpoint)
endtry
try
Xpath 'm'
call T41_G()
Xpath 'n'
catch /.*/
Xpath 'o'
call assert_report('Caught ' . v:exception . ' in ' . v:throwpoint)
endtry
try
Xpath 'p'
call T41_H()
Xpath 'q'
catch /.*/
Xpath 'r'
call assert_report('Caught ' . v:exception . ' in ' . v:throwpoint)
endtry
call assert_equal('jabckmdefnpghiq', g:Xpath)
endfunc
"-------------------------------------------------------------------------------
" Test 42: Catching number and string exceptions {{{1
"
" When a number is thrown, it is converted to a string exception.
" Numbers and strings may be caught by specifying a regular exception
" as argument to the :catch command.
"-------------------------------------------------------------------------------
func T42_F()
try
try
Xpath 'a'
throw 4711
Xpath 'b'
catch /4711/
Xpath 'c'
endtry
try
Xpath 'd'
throw 4711
Xpath 'e'
catch /^4711$/
Xpath 'f'
endtry
try
Xpath 'g'
throw 4711
Xpath 'h'
catch /\d/
Xpath 'i'
endtry
try
Xpath 'j'
throw 4711
Xpath 'k'
catch /^\d\+$/
Xpath 'l'
endtry
try
Xpath 'm'
throw "arrgh"
Xpath 'n'
catch /arrgh/
Xpath 'o'
endtry
try
Xpath 'p'
throw "arrgh"
Xpath 'q'
catch /^arrgh$/
Xpath 'r'
endtry
try
Xpath 's'
throw "arrgh"
Xpath 't'
catch /\l/
Xpath 'u'
endtry
try
Xpath 'v'
throw "arrgh"
Xpath 'w'
catch /^\l\+$/
Xpath 'x'
endtry
try
try
Xpath 'y'
throw "ARRGH"
Xpath 'z'
catch /^arrgh$/
Xpath 'A'
endtry
catch /^\carrgh$/
Xpath 'B'
endtry
try
Xpath 'C'
throw ""
Xpath 'D'
catch /^$/
Xpath 'E'
endtry
catch /.*/
Xpath 'F'
call assert_report('Caught ' . v:exception . ' in ' . v:throwpoint)
endtry
endfunc
func Test_catch_number_string()
XpathINIT
call T42_F()
call assert_equal('acdfgijlmoprsuvxyBCE', g:Xpath)
endfunc
"-------------------------------------------------------------------------------
" Test 43: Selecting the correct :catch clause {{{1
"
" When an exception is thrown and there are multiple :catch clauses,
" the first matching one is taken.
"-------------------------------------------------------------------------------
func T43_F()
let loops = 3
while loops > 0
try
if loops == 3
Xpath 'a' . loops
throw "a"
Xpath 'b' . loops
elseif loops == 2
Xpath 'c' . loops
throw "ab"
Xpath 'd' . loops
elseif loops == 1
Xpath 'e' . loops
throw "abc"
Xpath 'f' . loops
endif
catch /abc/
Xpath 'g' . loops
catch /ab/
Xpath 'h' . loops
catch /.*/
Xpath 'i' . loops
catch /a/
Xpath 'j' . loops
endtry
let loops = loops - 1
endwhile
Xpath 'k'
endfunc
func Test_multi_catch()
XpathINIT
call T43_F()
call assert_equal('a3i3c2h2e1g1k', g:Xpath)
endfunc
"-------------------------------------------------------------------------------
" Test 44: Missing or empty :catch patterns {{{1
"
" A missing or empty :catch pattern means the same as /.*/, that is,
" catches everything. To catch only empty exceptions, /^$/ must be
" used. A :catch with missing, empty, or /.*/ argument also works
" when followed by another command separated by a bar on the same
" line. :catch patterns cannot be specified between ||. But other
" pattern separators can be used instead of //.
"-------------------------------------------------------------------------------
func T44_F()
try
try
Xpath 'a'
throw ""
catch /^$/
Xpath 'b'
endtry
try
Xpath 'c'
throw ""
catch /.*/
Xpath 'd'
endtry
try
Xpath 'e'
throw ""
catch //
Xpath 'f'
endtry
try
Xpath 'g'
throw ""
catch
Xpath 'h'
endtry
try
Xpath 'i'
throw "oops"
catch /^$/
Xpath 'j'
catch /.*/
Xpath 'k'
endtry
try
Xpath 'l'
throw "arrgh"
catch /^$/
Xpath 'm'
catch //
Xpath 'n'
endtry
try
Xpath 'o'
throw "brrr"
catch /^$/
Xpath 'p'
catch
Xpath 'q'
endtry
try | Xpath 'r' | throw "x" | catch /.*/ | Xpath 's' | endtry
try | Xpath 't' | throw "y" | catch // | Xpath 'u' | endtry
while 1
try
let caught = 0
let v:errmsg = ""
" Extra try level: if ":catch" without arguments below raises
" a syntax error because it misinterprets the "Xpath" as a pattern,
" let it be caught by the ":catch /.*/" below.
try
try | Xpath 'v' | throw "z" | catch | Xpath 'w' | :
endtry
endtry
catch /.*/
let caught = 1
call assert_report('Caught ' . v:exception . ' in ' . v:throwpoint)
finally
if $VIMNOERRTHROW && v:errmsg != ""
call assert_report(v:errmsg)
endif
if caught || $VIMNOERRTHROW && v:errmsg != ""
Xpath 'x'
endif
break " discard error for $VIMNOERRTHROW
endtry
endwhile
let cologne = 4711
try
try
Xpath 'y'
throw "throw cologne"
" Next lines catches all and throws 4711:
catch |throw cologne|
Xpath 'z'
endtry
catch /4711/
Xpath 'A'
endtry
try
Xpath 'B'
throw "plus"
catch +plus+
Xpath 'C'
endtry
Xpath 'D'
catch /.*/
Xpath 'E'
call assert_report('Caught ' . v:exception . ' in ' . v:throwpoint)
endtry
endfunc
func Test_empty_catch()
XpathINIT
call T44_F()
call assert_equal('abcdefghiklnoqrstuvwyABCD', g:Xpath)
endfunc
"-------------------------------------------------------------------------------
" Test 45: Catching exceptions from nested :try blocks {{{1
"
" When :try blocks are nested, an exception is caught by the innermost
" try conditional that has a matching :catch clause.
"-------------------------------------------------------------------------------
func T45_F()
let loops = 3
while loops > 0
try
try
try
try
if loops == 3
Xpath 'a' . loops
throw "a"
Xpath 'b' . loops
elseif loops == 2
Xpath 'c' . loops
throw "ab"
Xpath 'd' . loops
elseif loops == 1
Xpath 'e' . loops
throw "abc"
Xpath 'f' . loops
endif
catch /abc/
Xpath 'g' . loops
endtry
catch /ab/
Xpath 'h' . loops
endtry
catch /.*/
Xpath 'i' . loops
endtry
catch /a/
Xpath 'j' . loops
endtry
let loops = loops - 1
endwhile
Xpath 'k'
endfunc
func Test_catch_from_nested_try()
XpathINIT
call T45_F()
call assert_equal('a3i3c2h2e1g1k', g:Xpath)
endfunc
"-------------------------------------------------------------------------------
" Test 46: Executing :finally after a :throw in nested :try {{{1
"
" When an exception is thrown from within nested :try blocks, the
" :finally clauses of the non-catching try conditionals should be
" executed before the matching :catch of the next surrounding :try
" gets the control. If this also has a :finally clause, it is
" executed afterwards.
"-------------------------------------------------------------------------------
func T46_F()
let sum = 0
try
Xpath 'a'
try
Xpath 'b'
try
Xpath 'c'
try
Xpath 'd'
throw "ABC"
Xpath 'e'
catch /xyz/
Xpath 'f'
finally
Xpath 'g'
if sum != 0
Xpath 'h'
endif
let sum = sum + 1
endtry
Xpath 'i'
catch /123/
Xpath 'j'
catch /321/
Xpath 'k'
finally
Xpath 'l'
if sum != 1
Xpath 'm'
endif
let sum = sum + 2
endtry
Xpath 'n'
finally
Xpath 'o'
if sum != 3
Xpath 'p'
endif
let sum = sum + 4
endtry
Xpath 'q'
catch /ABC/
Xpath 'r'
if sum != 7
Xpath 's'
endif
let sum = sum + 8
finally
Xpath 't'
if sum != 15
Xpath 'u'
endif
let sum = sum + 16
endtry
Xpath 'v'
if sum != 31
Xpath 'w'
endif
endfunc
func Test_finally_after_throw()
XpathINIT
call T46_F()
call assert_equal('abcdglortv', g:Xpath)
endfunc
"-------------------------------------------------------------------------------
" Test 47: Throwing exceptions from a :catch clause {{{1
"
" When an exception is thrown from a :catch clause, it should not be
" caught by a :catch of the same :try conditional. After executing
" the :finally clause (if present), surrounding try conditionals
" should be checked for a matching :catch.
"-------------------------------------------------------------------------------
func T47_F()
Xpath 'a'
try
Xpath 'b'
try
Xpath 'c'
try
Xpath 'd'
throw "x1"
Xpath 'e'
catch /x1/
Xpath 'f'
try
Xpath 'g'
throw "x2"
Xpath 'h'
catch /x1/
Xpath 'i'
catch /x2/
Xpath 'j'
try
Xpath 'k'
throw "x3"
Xpath 'l'
catch /x1/
Xpath 'm'
catch /x2/
Xpath 'n'
finally
Xpath 'o'
endtry
Xpath 'p'
catch /x3/
Xpath 'q'
endtry
Xpath 'r'
catch /x1/
Xpath 's'
catch /x2/
Xpath 't'
catch /x3/
Xpath 'u'
finally
Xpath 'v'
endtry
Xpath 'w'
catch /x1/
Xpath 'x'
catch /x2/
Xpath 'y'
catch /x3/
Xpath 'z'
endtry
Xpath 'A'
catch /.*/
Xpath 'B'
call assert_report('Caught ' . v:exception . ' in ' . v:throwpoint)
endtry
Xpath 'C'
endfunc
func Test_throw_from_catch()
XpathINIT
call T47_F()
call assert_equal('abcdfgjkovzAC', g:Xpath)
endfunc
"-------------------------------------------------------------------------------
" Test 48: Throwing exceptions from a :finally clause {{{1
"
" When an exception is thrown from a :finally clause, it should not be
" caught by a :catch of the same :try conditional. Surrounding try
" conditionals should be checked for a matching :catch. A previously
" thrown exception is discarded.
"-------------------------------------------------------------------------------
func T48_F()
try
try
try
Xpath 'a'
catch /x1/
Xpath 'b'
finally
Xpath 'c'
throw "x1"
Xpath 'd'
endtry
Xpath 'e'
catch /x1/
Xpath 'f'
endtry
Xpath 'g'
try
try
Xpath 'h'
throw "x2"
Xpath 'i'
catch /x2/
Xpath 'j'
catch /x3/
Xpath 'k'
finally
Xpath 'l'
throw "x3"
Xpath 'm'
endtry
Xpath 'n'
catch /x2/
Xpath 'o'
catch /x3/
Xpath 'p'
endtry
Xpath 'q'
try
try
try
Xpath 'r'
throw "x4"
Xpath 's'
catch /x5/
Xpath 't'
finally
Xpath 'u'
throw "x5" " discards 'x4'
Xpath 'v'
endtry
Xpath 'w'
catch /x4/
Xpath 'x'
finally
Xpath 'y'
endtry
Xpath 'z'
catch /x5/
Xpath 'A'
endtry
Xpath 'B'
catch /.*/
Xpath 'C'
call assert_report('Caught ' . v:exception . ' in ' . v:throwpoint)
endtry
Xpath 'D'
endfunc
func Test_throw_from_finally()
XpathINIT
call T48_F()
call assert_equal('acfghjlpqruyABD', g:Xpath)
endfunc
"-------------------------------------------------------------------------------
" Test 51: Throwing exceptions across :execute and user commands {{{1
"
" A :throw command may be executed under an ":execute" or from
" a user command.
"-------------------------------------------------------------------------------
func T51_F()
command! -nargs=? THROW1 throw <args> | throw 1
command! -nargs=? THROW2 try | throw <args> | endtry | throw 2
command! -nargs=? THROW3 try | throw 3 | catch /3/ | throw <args> | endtry
command! -nargs=? THROW4 try | throw 4 | finally | throw <args> | endtry
try
try
try
Xpath 'a'
THROW1 "A"
catch /A/
Xpath 'b'
endtry
catch /1/
Xpath 'c'
endtry
try
try
Xpath 'd'
THROW2 "B"
catch /B/
Xpath 'e'
endtry
catch /2/
Xpath 'f'
endtry
try
try
Xpath 'g'
THROW3 "C"
catch /C/
Xpath 'h'
endtry
catch /3/
Xpath 'i'
endtry
try
try
Xpath 'j'
THROW4 "D"
catch /D/
Xpath 'k'
endtry
catch /4/
Xpath 'l'
endtry
try
try
Xpath 'm'
execute 'throw "E" | throw 5'
catch /E/
Xpath 'n'
endtry
catch /5/
Xpath 'o'
endtry
try
try
Xpath 'p'
execute 'try | throw "F" | endtry | throw 6'
catch /F/
Xpath 'q'
endtry
catch /6/
Xpath 'r'
endtry
try
try
Xpath 's'
execute'try | throw 7 | catch /7/ | throw "G" | endtry'
catch /G/
Xpath 't'
endtry
catch /7/
Xpath 'u'
endtry
try
try
Xpath 'v'
execute 'try | throw 8 | finally | throw "H" | endtry'
catch /H/
Xpath 'w'
endtry
catch /8/
Xpath 'x'
endtry
catch /.*/
Xpath 'y'
call assert_report('Caught ' . v:exception . ' in ' . v:throwpoint)
endtry
Xpath 'z'
delcommand THROW1
delcommand THROW2
delcommand THROW3
delcommand THROW4
endfunc
func Test_throw_across_commands()
XpathINIT
call T51_F()
call assert_equal('abdeghjkmnpqstvwz', g:Xpath)
endfunc
"-------------------------------------------------------------------------------
" Test 69: :throw across :if, :elseif, :while {{{1
"
" On an :if, :elseif, or :while command, an exception might be thrown
" during evaluation of the expression to test. The exception can be
" caught by the script.
"-------------------------------------------------------------------------------
func T69_throw(x)
Xpath 'x'
throw a:x
endfunc
func Test_throw_ifelsewhile()
XpathINIT
try
try
Xpath 'a'
if 111 == T69_throw("if") + 111
Xpath 'b'
else
Xpath 'c'
endif
Xpath 'd'
catch /^if$/
Xpath 'e'
catch /.*/
Xpath 'f'
call assert_report("if: " . v:exception . " in " . v:throwpoint)
endtry
try
Xpath 'g'
if v:false
Xpath 'h'
elseif 222 == T69_throw("elseif") + 222
Xpath 'i'
else
Xpath 'j'
endif
Xpath 'k'
catch /^elseif$/
Xpath 'l'
catch /.*/
Xpath 'm'
call assert_report("elseif: " . v:exception . " in " . v:throwpoint)
endtry
try
Xpath 'n'
while 333 == T69_throw("while") + 333
Xpath 'o'
break
endwhile
Xpath 'p'
catch /^while$/
Xpath 'q'
catch /.*/
Xpath 'r'
call assert_report("while: " .. v:exception .. " in " .. v:throwpoint)
endtry
catch /^0$/ " default return value
Xpath 's'
call assert_report(v:throwpoint)
catch /.*/
call assert_report(v:exception .. " in " .. v:throwpoint)
Xpath 't'
endtry
call assert_equal('axegxlnxq', g:Xpath)
endfunc
"-------------------------------------------------------------------------------
" Test 70: :throw across :return or :throw {{{1
"
" On a :return or :throw command, an exception might be thrown during
" evaluation of the expression to return or throw, respectively. The
" exception can be caught by the script.
"-------------------------------------------------------------------------------
let T70_taken = ""
func T70_throw(x, n)
let g:T70_taken = g:T70_taken . "T" . a:n
throw a:x
endfunc
func T70_F(x, y, n)
let g:T70_taken = g:T70_taken . "F" . a:n
return a:x + T70_throw(a:y, a:n)
endfunc
func T70_G(x, y, n)
let g:T70_taken = g:T70_taken . "G" . a:n
throw a:x . T70_throw(a:y, a:n)
return a:x
endfunc
func Test_throwreturn()
XpathINIT
try
try
Xpath 'a'
call T70_F(4711, "return", 1)
Xpath 'b'
catch /^return$/
Xpath 'c'
catch /.*/
Xpath 'd'
call assert_report("return: " .. v:exception .. " in " .. v:throwpoint)
endtry
try
Xpath 'e'
let var = T70_F(4712, "return-var", 2)
Xpath 'f'
catch /^return-var$/
Xpath 'g'
catch /.*/
Xpath 'h'
call assert_report("return-var: " . v:exception . " in " . v:throwpoint)
finally
unlet! var
endtry
try
Xpath 'i'
throw "except1" . T70_throw("throw1", 3)
Xpath 'j'
catch /^except1/
Xpath 'k'
catch /^throw1$/
Xpath 'l'
catch /.*/
Xpath 'm'
call assert_report("throw1: " .. v:exception .. " in " .. v:throwpoint)
endtry
try
Xpath 'n'
call T70_G("except2", "throw2", 4)
Xpath 'o'
catch /^except2/
Xpath 'p'
catch /^throw2$/
Xpath 'q'
catch /.*/
Xpath 'r'
call assert_report("throw2: " .. v:exception .. " in " .. v:throwpoint)
endtry
try
Xpath 's'
let var = T70_G("except3", "throw3", 5)
Xpath 't'
catch /^except3/
Xpath 'u'
catch /^throw3$/
Xpath 'v'
catch /.*/
Xpath 'w'
call assert_report("throw3: " .. v:exception .. " in " .. v:throwpoint)
finally
unlet! var
endtry
call assert_equal('F1T1F2T2T3G4T4G5T5', g:T70_taken)
Xpath 'x'
catch /^0$/ " default return value
Xpath 'y'
call assert_report(v:throwpoint)
catch /.*/
Xpath 'z'
call assert_report('Caught' .. v:exception .. ' in ' .. v:throwpoint)
endtry
call assert_equal('acegilnqsvx', g:Xpath)
endfunc
"-------------------------------------------------------------------------------
" Test 71: :throw across :echo variants and :execute {{{1
"
" On an :echo, :echon, :echomsg, :echoerr, or :execute command, an
" exception might be thrown during evaluation of the arguments to
" be displayed or executed as a command, respectively. Any following
" arguments are not evaluated, then. The exception can be caught by
" the script.
"-------------------------------------------------------------------------------
let T71_taken = ""
func T71_throw(x, n)
let g:T71_taken = g:T71_taken . "T" . a:n
throw a:x
endfunc
func T71_F(n)
let g:T71_taken = g:T71_taken . "F" . a:n
return "F" . a:n
endfunc
func Test_throw_echo()
XpathINIT
try
try
Xpath 'a'
echo 'echo ' . T71_throw("echo-except", 1) . T71_F(1)
Xpath 'b'
catch /^echo-except$/
Xpath 'c'
catch /.*/
Xpath 'd'
call assert_report("echo: " .. v:exception .. " in " .. v:throwpoint)
endtry
try
Xpath 'e'
echon "echon " . T71_throw("echon-except", 2) . T71_F(2)
Xpath 'f'
catch /^echon-except$/
Xpath 'g'
catch /.*/
Xpath 'h'
call assert_report('echon: ' . v:exception . ' in ' . v:throwpoint)
endtry
try
Xpath 'i'
echomsg "echomsg " . T71_throw("echomsg-except", 3) . T71_F(3)
Xpath 'j'
catch /^echomsg-except$/
Xpath 'k'
catch /.*/
Xpath 'l'
call assert_report('echomsg: ' . v:exception . ' in ' . v:throwpoint)
endtry
try
Xpath 'm'
echoerr "echoerr " . T71_throw("echoerr-except", 4) . T71_F(4)
Xpath 'n'
catch /^echoerr-except$/
Xpath 'o'
catch /Vim/
Xpath 'p'
catch /echoerr/
Xpath 'q'
catch /.*/
Xpath 'r'
call assert_report('echoerr: ' . v:exception . ' in ' . v:throwpoint)
endtry
try
Xpath 's'
execute "echo 'execute " . T71_throw("execute-except", 5) . T71_F(5) "'"
Xpath 't'
catch /^execute-except$/
Xpath 'u'
catch /.*/
Xpath 'v'
call assert_report('execute: ' . v:exception . ' in ' . v:throwpoint)
endtry
call assert_equal('T1T2T3T4T5', g:T71_taken)
Xpath 'w'
catch /^0$/ " default return value
Xpath 'x'
call assert_report(v:throwpoint)
catch /.*/
Xpath 'y'
call assert_report('Caught ' . v:exception . ' in ' . v:throwpoint)
endtry
call assert_equal('acegikmosuw', g:Xpath)
endfunc
"-------------------------------------------------------------------------------
" Test 72: :throw across :let or :unlet {{{1
"
" On a :let command, an exception might be thrown during evaluation
" of the expression to assign. On an :let or :unlet command, the
" evaluation of the name of the variable to be assigned or list or
" deleted, respectively, may throw an exception. Any following
" arguments are not evaluated, then. The exception can be caught by
" the script.
"-------------------------------------------------------------------------------
let throwcount = 0
func T72_throw(x)
let g:throwcount = g:throwcount + 1
throw a:x
endfunc
let T72_addpath = ''
func T72_addpath(p)
let g:T72_addpath = g:T72_addpath . a:p
endfunc
func Test_throw_let()
XpathINIT
try
try
let $VAR = 'old_value'
Xpath 'a'
let $VAR = 'let(' . T72_throw('var') . ')'
Xpath 'b'
catch /^var$/
Xpath 'c'
finally
call assert_equal('old_value', $VAR)
endtry
try
let @a = 'old_value'
Xpath 'd'
let @a = 'let(' . T72_throw('reg') . ')'
Xpath 'e'
catch /^reg$/
try
Xpath 'f'
let @A = 'let(' . T72_throw('REG') . ')'
Xpath 'g'
catch /^REG$/
Xpath 'h'
endtry
finally
call assert_equal('old_value', @a)
call assert_equal('old_value', @A)
endtry
try
let saved_gpath = &g:path
let saved_lpath = &l:path
Xpath 'i'
let &path = 'let(' . T72_throw('opt') . ')'
Xpath 'j'
catch /^opt$/
try
Xpath 'k'
let &g:path = 'let(' . T72_throw('gopt') . ')'
Xpath 'l'
catch /^gopt$/
try
Xpath 'm'
let &l:path = 'let(' . T72_throw('lopt') . ')'
Xpath 'n'
catch /^lopt$/
Xpath 'o'
endtry
endtry
finally
call assert_equal(saved_gpath, &g:path)
call assert_equal(saved_lpath, &l:path)
let &g:path = saved_gpath
let &l:path = saved_lpath
endtry
unlet! var1 var2 var3
try
Xpath 'p'
let var1 = 'let(' . T72_throw('var1') . ')'
Xpath 'q'
catch /^var1$/
Xpath 'r'
finally
call assert_true(!exists('var1'))
endtry
try
let var2 = 'old_value'
Xpath 's'
let var2 = 'let(' . T72_throw('var2'). ')'
Xpath 't'
catch /^var2$/
Xpath 'u'
finally
call assert_equal('old_value', var2)
endtry
try
Xpath 'v'
let var{T72_throw('var3')} = 4711
Xpath 'w'
catch /^var3$/
Xpath 'x'
endtry
try
call T72_addpath('T1')
let var{T72_throw('var4')} var{T72_addpath('T2')} | call T72_addpath('T3')
call T72_addpath('T4')
catch /^var4$/
call T72_addpath('T5')
endtry
try
call T72_addpath('T6')
unlet var{T72_throw('var5')} var{T72_addpath('T7')}
\ | call T72_addpath('T8')
call T72_addpath('T9')
catch /^var5$/
call T72_addpath('T10')
endtry
call assert_equal('T1T5T6T10', g:T72_addpath)
call assert_equal(11, g:throwcount)
catch /.*/
call assert_report('Caught ' . v:exception . ' in ' . v:throwpoint)
endtry
call assert_equal('acdfhikmoprsuvx', g:Xpath)
endfunc
"-------------------------------------------------------------------------------
" Test 73: :throw across :function, :delfunction {{{1
"
" The :function and :delfunction commands may cause an expression
" specified in braces to be evaluated. During evaluation, an
" exception might be thrown. The exception can be caught by the
" script.
"-------------------------------------------------------------------------------
let T73_taken = ''
func T73_throw(x, n)
let g:T73_taken = g:T73_taken . 'T' . a:n
throw a:x
endfunc
func T73_expr(x, n)
let g:T73_taken = g:T73_taken . 'E' . a:n
if a:n % 2 == 0
call T73_throw(a:x, a:n)
endif
return 2 - a:n % 2
endfunc
func Test_throw_func()
XpathINIT
try
try
" Define function.
Xpath 'a'
function! F0()
endfunction
Xpath 'b'
function! F{T73_expr('function-def-ok', 1)}()
endfunction
Xpath 'c'
function! F{T73_expr('function-def', 2)}()
endfunction
Xpath 'd'
catch /^function-def-ok$/
Xpath 'e'
catch /^function-def$/
Xpath 'f'
catch /.*/
call assert_report('def: ' . v:exception . ' in ' . v:throwpoint)
endtry
try
" List function.
Xpath 'g'
function F0
Xpath 'h'
function F{T73_expr('function-lst-ok', 3)}
Xpath 'i'
function F{T73_expr('function-lst', 4)}
Xpath 'j'
catch /^function-lst-ok$/
Xpath 'k'
catch /^function-lst$/
Xpath 'l'
catch /.*/
call assert_report('lst: ' . v:exception . ' in ' . v:throwpoint)
endtry
try
" Delete function
Xpath 'm'
delfunction F0
Xpath 'n'
delfunction F{T73_expr('function-del-ok', 5)}
Xpath 'o'
delfunction F{T73_expr('function-del', 6)}
Xpath 'p'
catch /^function-del-ok$/
Xpath 'q'
catch /^function-del$/
Xpath 'r'
catch /.*/
call assert_report('del: ' . v:exception . ' in ' . v:throwpoint)
endtry
call assert_equal('E1E2T2E3E4T4E5E6T6', g:T73_taken)
catch /.*/
call assert_report('Caught ' . v:exception . ' in ' . v:throwpoint)
endtry
call assert_equal('abcfghilmnor', g:Xpath)
endfunc
"-------------------------------------------------------------------------------
" Test 74: :throw across builtin functions and commands {{{1
"
" Some functions like exists(), searchpair() take expression
" arguments, other functions or commands like substitute() or
" :substitute cause an expression (specified in the regular
" expression) to be evaluated. During evaluation an exception
" might be thrown. The exception can be caught by the script.
"-------------------------------------------------------------------------------
let T74_taken = ""
func T74_throw(x, n)
let g:T74_taken = g:T74_taken . "T" . a:n
throw a:x
endfunc
func T74_expr(x, n)
let g:T74_taken = g:T74_taken . "E" . a:n
call T74_throw(a:x . a:n, a:n)
return "EXPR"
endfunc
func T74_skip(x, n)
let g:T74_taken = g:T74_taken . "S" . a:n . "(" . line(".")
let theline = getline(".")
if theline =~ "skip"
let g:T74_taken = g:T74_taken . "s)"
return 1
elseif theline =~ "throw"
let g:T74_taken = g:T74_taken . "t)"
call T74_throw(a:x . a:n, a:n)
else
let g:T74_taken = g:T74_taken . ")"
return 0
endif
endfunc
func T74_subst(x, n)
let g:T74_taken = g:T74_taken . "U" . a:n . "(" . line(".")
let theline = getline(".")
if theline =~ "not" " T74_subst() should not be called for this line
let g:T74_taken = g:T74_taken . "n)"
call T74_throw(a:x . a:n, a:n)
elseif theline =~ "throw"
let g:T74_taken = g:T74_taken . "t)"
call T74_throw(a:x . a:n, a:n)
else
let g:T74_taken = g:T74_taken . ")"
return "replaced"
endif
endfunc
func Test_throw_builtin_func()
XpathINIT
try
try
Xpath 'a'
let result = exists('*{T74_expr("exists", 1)}')
Xpath 'b'
catch /^exists1$/
Xpath 'c'
try
let result = exists('{T74_expr("exists", 2)}')
Xpath 'd'
catch /^exists2$/
Xpath 'e'
catch /.*/
call assert_report('exists2: ' . v:exception . ' in ' . v:throwpoint)
endtry
catch /.*/
call assert_report('exists1: ' . v:exception . ' in ' . v:throwpoint)
endtry
try
let file = tempname()
exec "edit" file
call append(0, [
\ 'begin',
\ 'xx',
\ 'middle 3',
\ 'xx',
\ 'middle 5 skip',
\ 'xx',
\ 'middle 7 throw',
\ 'xx',
\ 'end'])
normal! gg
Xpath 'f'
let result = searchpair("begin", "middle", "end", '',
\ 'T74_skip("searchpair", 3)')
Xpath 'g'
let result = searchpair("begin", "middle", "end", '',
\ 'T74_skip("searchpair", 4)')
Xpath 'h'
let result = searchpair("begin", "middle", "end", '',
\ 'T74_skip("searchpair", 5)')
Xpath 'i'
catch /^searchpair[35]$/
Xpath 'j'
catch /^searchpair4$/
Xpath 'k'
catch /.*/
call assert_report('searchpair: ' . v:exception . ' in ' . v:throwpoint)
finally
bwipeout!
call delete(file)
endtry
try
let file = tempname()
exec "edit" file
call append(0, [
\ 'subst 1',
\ 'subst 2',
\ 'not',
\ 'subst 4',
\ 'subst throw',
\ 'subst 6'])
normal! gg
Xpath 'l'
1,2substitute/subst/\=T74_subst("substitute", 6)/
try
Xpath 'm'
try
let v:errmsg = ""
3substitute/subst/\=T74_subst("substitute", 7)/
finally
if v:errmsg != ""
" If exceptions are not thrown on errors, fake the error
" exception in order to get the same execution path.
throw "faked Vim(substitute)"
endif
endtry
catch /Vim(substitute)/ " Pattern not found ('e' flag missing)
Xpath 'n'
3substitute/subst/\=T74_subst("substitute", 8)/e
Xpath 'o'
endtry
Xpath 'p'
4,6substitute/subst/\=T74_subst("substitute", 9)/
Xpath 'q'
catch /^substitute[678]/
Xpath 'r'
catch /^substitute9/
Xpath 's'
finally
bwipeout!
call delete(file)
endtry
try
Xpath 't'
let var = substitute("sub", "sub", '\=T74_throw("substitute()y", 10)', '')
Xpath 'u'
catch /substitute()y/
Xpath 'v'
catch /.*/
call assert_report('substitute()y: ' . v:exception . ' in '
\ . v:throwpoint)
endtry
try
Xpath 'w'
let var = substitute("not", "sub", '\=T74_throw("substitute()n", 11)', '')
Xpath 'x'
catch /substitute()n/
Xpath 'y'
catch /.*/
call assert_report('substitute()n: ' . v:exception . ' in '
\ . v:throwpoint)
endtry
call assert_equal('E1T1E2T2S3(3)S4(5s)S4(7t)T4U6(1)U6(2)U9(4)U9(5t)T9T10',
\ g:T74_taken)
catch /.*/
call assert_report('Caught ' . v:exception . ' in ' . v:throwpoint)
endtry
call assert_equal('acefgklmnopstvwx', g:Xpath)
endfunc
"-------------------------------------------------------------------------------
" Test 75: Errors in builtin functions. {{{1
"
" On an error in a builtin function called inside a :try/:endtry
" region, the evaluation of the expression calling that function and
" the command containing that expression are abandoned. The error can
" be caught as an exception.
"
" A simple :call of the builtin function is a trivial case. If the
" builtin function is called in the argument list of another function,
" no further arguments are evaluated, and the other function is not
" executed. If the builtin function is called from the argument of
" a :return command, the :return command is not executed. If the
" builtin function is called from the argument of a :throw command,
" the :throw command is not executed. The evaluation of the
" expression calling the builtin function is abandoned.
"-------------------------------------------------------------------------------
func T75_F1(arg1)
Xpath 'a'
endfunc
func T75_F2(arg1, arg2)
Xpath 'b'
endfunc
func T75_G()
Xpath 'c'
endfunc
func T75_H()
Xpath 'd'
endfunc
func T75_R()
while 1
try
let caught = 0
let v:errmsg = ""
Xpath 'e'
return append(1, "s")
catch /E21/
let caught = 1
catch /.*/
Xpath 'f'
finally
Xpath 'g'
if caught || $VIMNOERRTHROW && v:errmsg =~ 'E21'
Xpath 'h'
endif
break " discard error for $VIMNOERRTHROW
endtry
endwhile
Xpath 'i'
endfunc
func Test_builtin_func_error()
XpathINIT
try
set noma " let append() fail with "E21"
while 1
try
let caught = 0
let v:errmsg = ""
Xpath 'j'
call append(1, "s")
catch /E21/
let caught = 1
catch /.*/
Xpath 'k'
finally
Xpath 'l'
if caught || $VIMNOERRTHROW && v:errmsg =~ 'E21'
Xpath 'm'
endif
break " discard error for $VIMNOERRTHROW
endtry
endwhile
while 1
try
let caught = 0
let v:errmsg = ""
Xpath 'n'
call T75_F1('x' . append(1, "s"))
catch /E21/
let caught = 1
catch /.*/
Xpath 'o'
finally
Xpath 'p'
if caught || $VIMNOERRTHROW && v:errmsg =~ 'E21'
Xpath 'q'
endif
break " discard error for $VIMNOERRTHROW
endtry
endwhile
while 1
try
let caught = 0
let v:errmsg = ""
Xpath 'r'
call T75_F2('x' . append(1, "s"), T75_G())
catch /E21/
let caught = 1
catch /.*/
Xpath 's'
finally
Xpath 't'
if caught || $VIMNOERRTHROW && v:errmsg =~ 'E21'
Xpath 'u'
endif
break " discard error for $VIMNOERRTHROW
endtry
endwhile
call T75_R()
while 1
try
let caught = 0
let v:errmsg = ""
Xpath 'v'
throw "T" . append(1, "s")
catch /E21/
let caught = 1
catch /^T.*/
Xpath 'w'
catch /.*/
Xpath 'x'
finally
Xpath 'y'
if caught || $VIMNOERRTHROW && v:errmsg =~ 'E21'
Xpath 'z'
endif
break " discard error for $VIMNOERRTHROW
endtry
endwhile
while 1
try
let caught = 0
let v:errmsg = ""
Xpath 'A'
let x = "a"
let x = x . "b" . append(1, "s") . T75_H()
catch /E21/
let caught = 1
catch /.*/
Xpath 'B'
finally
Xpath 'C'
if caught || $VIMNOERRTHROW && v:errmsg =~ 'E21'
Xpath 'D'
endif
call assert_equal('a', x)
break " discard error for $VIMNOERRTHROW
endtry
endwhile
catch /.*/
call assert_report('Caught ' . v:exception . ' in ' . v:throwpoint)
finally
set ma&
endtry
call assert_equal('jlmnpqrtueghivyzACD', g:Xpath)
endfunc
func Test_reload_in_try_catch()
call writefile(['x'], 'Xreload')
set autoread
edit Xreload
tabnew
call writefile(['xx'], 'Xreload')
augroup ReLoad
au FileReadPost Xreload let x = doesnotexist
au BufReadPost Xreload let x = doesnotexist
augroup END
try
edit Xreload
catch
endtry
tabnew
tabclose
tabclose
autocmd! ReLoad
set noautoread
bwipe! Xreload
call delete('Xreload')
endfunc
" Test for errors with :catch, :throw, :finally {{{1
func Test_try_catch_errors()
call assert_fails('throw |', 'E471:')
call assert_fails("throw \n ", 'E471:')
call assert_fails('catch abc', 'E654:')
call assert_fails('try | let i = 1| finally | catch | endtry', 'E604:')
call assert_fails('finally', 'E606:')
call assert_fails('try | finally | finally | endtry', 'E607:')
call assert_fails('try | for i in range(5) | endif | endtry', 'E580:')
call assert_fails('try | while v:true | endtry', 'E170:')
call assert_fails('try | if v:true | endtry', 'E171:')
endfunc
" Test for verbose messages with :try :catch, and :finally {{{1
func Test_try_catch_verbose()
" This test works only when the language is English
CheckEnglish
set verbose=14
" Test for verbose messages displayed when an exception is caught
redir => msg
try
echo i
catch /E121:/
finally
endtry
redir END
let expected = [
\ 'Exception thrown: Vim(echo):E121: Undefined variable: i', '',
\ 'Exception caught: Vim(echo):E121: Undefined variable: i', '',
\ 'Exception finished: Vim(echo):E121: Undefined variable: i']
call assert_equal(expected, split(msg, "\n"))
" Test for verbose messages displayed when an exception is discarded
redir => msg
try
try
throw 'abc'
finally
throw 'xyz'
endtry
catch
endtry
redir END
let expected = [
\ 'Exception thrown: abc', '',
\ 'Exception made pending: abc', '',
\ 'Exception thrown: xyz', '',
\ 'Exception discarded: abc', '',
\ 'Exception caught: xyz', '',
\ 'Exception finished: xyz']
call assert_equal(expected, split(msg, "\n"))
" Test for messages displayed when :throw is resumed after :finally
redir => msg
try
try
throw 'abc'
finally
endtry
catch
endtry
redir END
let expected = [
\ 'Exception thrown: abc', '',
\ 'Exception made pending: abc', '',
\ 'Exception resumed: abc', '',
\ 'Exception caught: abc', '',
\ 'Exception finished: abc']
call assert_equal(expected, split(msg, "\n"))
" Test for messages displayed when :break is resumed after :finally
redir => msg
for i in range(1)
try
break
finally
endtry
endfor
redir END
let expected = [':break made pending', '', ':break resumed']
call assert_equal(expected, split(msg, "\n"))
" Test for messages displayed when :continue is resumed after :finally
redir => msg
for i in range(1)
try
continue
finally
endtry
endfor
redir END
let expected = [':continue made pending', '', ':continue resumed']
call assert_equal(expected, split(msg, "\n"))
" Test for messages displayed when :return is resumed after :finally
func Xtest()
try
return 'vim'
finally
endtry
endfunc
redir => msg
call Xtest()
redir END
let expected = [
\ 'calling Xtest()', '',
\ ':return vim made pending', '',
\ ':return vim resumed', '',
\ 'Xtest returning ''vim''', '',
\ 'continuing in Test_try_catch_verbose']
call assert_equal(expected, split(msg, "\n"))
delfunc Xtest
" Test for messages displayed when :finish is resumed after :finally
call writefile(['try', 'finish', 'finally', 'endtry'], 'Xscript')
redir => msg
source Xscript
redir END
let expected = [
\ ':finish made pending', '',
\ ':finish resumed', '',
\ 'finished sourcing Xscript',
\ 'continuing in Test_try_catch_verbose']
call assert_equal(expected, split(msg, "\n")[1:])
call delete('Xscript')
" Test for messages displayed when a pending :continue is discarded by an
" exception in a finally handler
redir => msg
try
for i in range(1)
try
continue
finally
throw 'abc'
endtry
endfor
catch
endtry
redir END
let expected = [
\ ':continue made pending', '',
\ 'Exception thrown: abc', '',
\ ':continue discarded', '',
\ 'Exception caught: abc', '',
\ 'Exception finished: abc']
call assert_equal(expected, split(msg, "\n"))
set verbose&
endfunc
" Test for throwing an exception from a BufEnter autocmd {{{1
func Test_BufEnter_exception()
augroup bufenter_exception
au!
autocmd BufEnter Xfile1 throw 'abc'
augroup END
let caught_abc = 0
try
sp Xfile1
catch /^abc/
let caught_abc = 1
endtry
call assert_equal(1, caught_abc)
call assert_equal(1, winnr('$'))
augroup bufenter_exception
au!
augroup END
augroup! bufenter_exception
%bwipe!
" Test for recursively throwing exceptions in autocmds
augroup bufenter_exception
au!
autocmd BufEnter Xfile1 throw 'bufenter'
autocmd BufLeave Xfile1 throw 'bufleave'
augroup END
let ex_count = 0
try
try
sp Xfile1
catch /^bufenter/
let ex_count += 1
endtry
catch /^bufleave/
let ex_count += 10
endtry
call assert_equal(10, ex_count)
call assert_equal(2, winnr('$'))
augroup bufenter_exception
au!
augroup END
augroup! bufenter_exception
%bwipe!
endfunc
" Test for using try/catch in a user command with a failing expression {{{1
func Test_user_command_try_catch()
let lines =<< trim END
function s:throw() abort
throw 'error'
endfunction
command! Execute
\ try
\ | let s:x = s:throw()
\ | catch
\ | let g:caught = 'caught'
\ | endtry
let g:caught = 'no'
Execute
call assert_equal('caught', g:caught)
END
call writefile(lines, 'XtestTryCatch')
source XtestTryCatch
call delete('XtestTryCatch')
unlet g:caught
endfunc
" Test for using throw in a called function with following error {{{1
func Test_user_command_throw_in_function_call()
let lines =<< trim END
function s:get_dict() abort
throw 'my_error'
endfunction
try
call s:get_dict().foo()
catch /my_error/
let caught = 'yes'
catch
let caught = v:exception
endtry
call assert_equal('yes', caught)
END
call writefile(lines, 'XtestThrow')
source XtestThrow
call delete('XtestThrow')
unlet g:caught
endfunc
" Test that after reporting an uncaught exception there is no error for a
" missing :endif
func Test_after_exception_no_endif_error()
function Throw()
throw "Failure"
endfunction
function Foo()
if 1
call Throw()
endif
endfunction
call assert_fails('call Foo()', ['E605:', 'E605:'])
delfunc Throw
delfunc Foo
endfunc
" Test for using throw in a called function with following endtry {{{1
func Test_user_command_function_call_with_endtry()
let lines =<< trim END
funct s:throw(msg) abort
throw a:msg
endfunc
func s:main() abort
try
try
throw 'err1'
catch
call s:throw('err2') | endtry
catch
let s:caught = 'yes'
endtry
endfunc
call s:main()
call assert_equal('yes', s:caught)
END
call writefile(lines, 'XtestThrow')
source XtestThrow
call delete('XtestThrow')
endfunc
func ThisWillFail()
endfunc
" This was crashing prior to the fix in 8.2.3478.
func Test_error_in_catch_and_finally()
let lines =<< trim END
try
echo x
catch
for l in []
finally
END
call writefile(lines, 'XtestCatchAndFinally')
try
source XtestCatchAndFinally
catch /E600:/
endtry
call delete('XtestCatchAndFinally')
endfunc
" This was causing an illegal memory access
func Test_leave_block_in_endtry_not_called()
let lines =<< trim END
vim9script
try #
for x in []
if
endwhile
if
endtry
END
call writefile(lines, 'XtestEndtry')
try
source XtestEndtry
catch /E171:/
endtry
call delete('XtestEndtry')
endfunc
" Modeline {{{1
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
|