texception3.pp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. {$mode objfpc}
  2. uses
  3. erroru,sysutils;
  4. var
  5. i : longint;
  6. procedure test1;
  7. begin
  8. try
  9. i:=0;
  10. exit;
  11. finally
  12. inc(i);
  13. end;
  14. i:=-2;
  15. end;
  16. procedure test2;
  17. begin
  18. try
  19. i:=0;
  20. raise exception.create('');
  21. finally
  22. inc(i);
  23. end;
  24. i:=-2;
  25. end;
  26. procedure test3;
  27. begin
  28. try
  29. try
  30. i:=0;
  31. raise exception.create('');
  32. finally
  33. inc(i);
  34. end;
  35. finally
  36. inc(i);
  37. end;
  38. i:=-2;
  39. end;
  40. procedure test4;
  41. begin
  42. try
  43. try
  44. i:=0;
  45. exit;
  46. finally
  47. inc(i);
  48. end;
  49. finally
  50. inc(i);
  51. end;
  52. i:=-2;
  53. end;
  54. procedure test5;
  55. var
  56. j : longint;
  57. begin
  58. for j:=1 to 10 do
  59. begin
  60. try
  61. i:=0;
  62. break;
  63. finally
  64. inc(i);
  65. end;
  66. dec(i);
  67. end;
  68. end;
  69. procedure test6;
  70. var
  71. j : longint;
  72. begin
  73. i:=0;
  74. for j:=1 to 10 do
  75. begin
  76. try
  77. continue;
  78. finally
  79. inc(i);
  80. end;
  81. dec(i);
  82. end;
  83. end;
  84. procedure test7;
  85. var
  86. j : longint;
  87. begin
  88. for j:=1 to 10 do
  89. begin
  90. try
  91. try
  92. i:=0;
  93. break;
  94. finally
  95. inc(i);
  96. end;
  97. dec(i);
  98. finally
  99. inc(i);
  100. end;
  101. end;
  102. end;
  103. procedure test8;
  104. var
  105. j : longint;
  106. begin
  107. i:=0;
  108. for j:=1 to 10 do
  109. begin
  110. try
  111. try
  112. continue;
  113. finally
  114. inc(i);
  115. end;
  116. finally
  117. inc(i);
  118. end;
  119. dec(i);
  120. end;
  121. end;
  122. { some combined test ... }
  123. procedure test9;
  124. var
  125. j : longint;
  126. begin
  127. try
  128. i:=0;
  129. finally
  130. for j:=1 to 10 do
  131. begin
  132. try
  133. if j<2 then
  134. continue
  135. else
  136. break;
  137. finally
  138. inc(i);
  139. end;
  140. dec(i);
  141. end;
  142. end;
  143. end;
  144. procedure test10;
  145. var
  146. j : longint;
  147. begin
  148. try
  149. i:=0;
  150. j:=1;
  151. finally
  152. while j<=10 do
  153. begin
  154. try
  155. if j<2 then
  156. continue
  157. else
  158. break;
  159. finally
  160. inc(i);
  161. inc(j);
  162. end;
  163. dec(i);
  164. end;
  165. end;
  166. end;
  167. { the do_raise function is a little bit more complicated }
  168. { so we also check if memory is lost }
  169. function do_raise : ansistring;
  170. var
  171. a1,a2 : ansistring;
  172. j : longint;
  173. begin
  174. for j:=1 to 3 do
  175. begin
  176. a1:=copy('Hello world',1,5);
  177. do_raise:=copy(a2,1,1);
  178. end;
  179. raise exception.create('A string to test memory allocation');
  180. do_error(99998);
  181. end;
  182. { now test real exceptions }
  183. procedure test100;
  184. begin
  185. try
  186. i:=0;
  187. do_raise;
  188. except
  189. inc(i);
  190. end;
  191. end;
  192. procedure test101;
  193. begin
  194. try
  195. try
  196. i:=0;
  197. do_raise;
  198. except
  199. inc(i);
  200. do_raise;
  201. end;
  202. except
  203. inc(i);
  204. end;
  205. end;
  206. procedure test102;
  207. begin
  208. try
  209. try
  210. i:=0;
  211. do_raise;
  212. except
  213. inc(i);
  214. raise;
  215. end;
  216. except
  217. inc(i);
  218. end;
  219. end;
  220. { tests continue in try...except...end; statements }
  221. procedure test103;
  222. var
  223. j,k : longint;
  224. begin
  225. i:=0;
  226. for j:=1 to 10 do
  227. try
  228. for k:=1 to 10 do
  229. try
  230. inc(i);
  231. if (i mod 10)>5 then
  232. do_raise
  233. else
  234. continue;
  235. except
  236. continue
  237. end;
  238. if i>50 then
  239. do_raise
  240. else
  241. continue;
  242. except
  243. continue;
  244. end;
  245. end;
  246. procedure test104;
  247. begin
  248. try
  249. i:=1;
  250. exit;
  251. // we should never get there
  252. do_raise;
  253. except
  254. i:=-1;
  255. end;
  256. i:=-2;
  257. end;
  258. procedure test105;
  259. begin
  260. try
  261. i:=0;
  262. do_raise;
  263. // we should never get there
  264. i:=-1;
  265. except
  266. inc(i);
  267. exit;
  268. end;
  269. end;
  270. procedure test106;
  271. begin
  272. try
  273. try
  274. i:=1;
  275. exit;
  276. // we should never get there
  277. do_raise;
  278. except
  279. i:=-1;
  280. end;
  281. i:=-2;
  282. except
  283. end;
  284. end;
  285. procedure test107;
  286. begin
  287. try
  288. do_raise;
  289. except
  290. try
  291. i:=0;
  292. do_raise;
  293. // we should never get there
  294. i:=-1;
  295. except
  296. inc(i);
  297. exit;
  298. end;
  299. end;
  300. end;
  301. { tests break in try...except...end; statements }
  302. procedure test108;
  303. begin
  304. i:=0;
  305. while true do
  306. try
  307. while true do
  308. try
  309. inc(i);
  310. break;
  311. except
  312. end;
  313. inc(i);
  314. break;
  315. except
  316. end;
  317. end;
  318. procedure test109;
  319. begin
  320. i:=0;
  321. while true do
  322. try
  323. repeat
  324. try
  325. do_raise;
  326. i:=-1;
  327. except
  328. inc(i);
  329. break;
  330. end;
  331. until false;
  332. do_raise;
  333. i:=-1;
  334. except
  335. inc(i);
  336. break;
  337. end;
  338. end;
  339. { test the on statement }
  340. procedure test110;
  341. begin
  342. try
  343. i:=0;
  344. do_raise;
  345. except
  346. on e : exception do
  347. inc(i);
  348. end;
  349. end;
  350. procedure test111;
  351. begin
  352. try
  353. try
  354. i:=0;
  355. do_raise;
  356. except
  357. on e : exception do
  358. begin
  359. inc(i);
  360. do_raise;
  361. end;
  362. end;
  363. except
  364. on e : exception do
  365. inc(i);
  366. end;
  367. end;
  368. procedure test112;
  369. begin
  370. try
  371. try
  372. i:=0;
  373. do_raise;
  374. except
  375. on e : exception do
  376. begin
  377. inc(i);
  378. raise;
  379. end;
  380. end;
  381. except
  382. on e : exception do
  383. inc(i);
  384. end;
  385. end;
  386. procedure test113;
  387. var
  388. j,k : longint;
  389. begin
  390. i:=0;
  391. for j:=1 to 10 do
  392. try
  393. for k:=1 to 10 do
  394. try
  395. inc(i);
  396. if (i mod 10)>5 then
  397. do_raise
  398. else
  399. continue;
  400. except
  401. on e : exception do
  402. continue
  403. end;
  404. if i>50 then
  405. do_raise
  406. else
  407. continue;
  408. except
  409. on e : exception do
  410. continue;
  411. end;
  412. end;
  413. procedure test114;
  414. begin
  415. try
  416. i:=1;
  417. exit;
  418. // we should never get there
  419. do_raise;
  420. except
  421. on e : exception do
  422. i:=-1;
  423. end;
  424. i:=-2;
  425. end;
  426. procedure test115;
  427. begin
  428. try
  429. i:=0;
  430. do_raise;
  431. // we should never get there
  432. i:=-1;
  433. except
  434. on e : exception do
  435. begin
  436. inc(i);
  437. exit;
  438. end;
  439. end;
  440. end;
  441. procedure test116;
  442. begin
  443. try
  444. try
  445. i:=1;
  446. exit;
  447. // we should never get there
  448. do_raise;
  449. except
  450. on e : exception do
  451. i:=-1;
  452. end;
  453. i:=-2;
  454. except
  455. on e : exception do
  456. ;
  457. end;
  458. end;
  459. procedure test117;
  460. begin
  461. try
  462. do_raise;
  463. except
  464. try
  465. i:=0;
  466. do_raise;
  467. // we should never get there
  468. i:=-1;
  469. except
  470. on e : exception do
  471. begin
  472. inc(i);
  473. exit;
  474. end;
  475. end;
  476. end;
  477. end;
  478. { tests break in try...except...end; statements }
  479. procedure test118;
  480. begin
  481. i:=0;
  482. while true do
  483. try
  484. while true do
  485. try
  486. inc(i);
  487. break;
  488. except
  489. on e : exception do
  490. ;
  491. end;
  492. inc(i);
  493. break;
  494. except
  495. on e : exception do
  496. ;
  497. end;
  498. end;
  499. procedure test119;
  500. begin
  501. i:=0;
  502. while true do
  503. try
  504. repeat
  505. try
  506. do_raise;
  507. i:=-1;
  508. except
  509. on e : exception do
  510. begin
  511. inc(i);
  512. break;
  513. end;
  514. end;
  515. until false;
  516. do_raise;
  517. i:=-1;
  518. except
  519. on e : exception do
  520. begin
  521. inc(i);
  522. break;
  523. end;
  524. end;
  525. end;
  526. var
  527. mem : sizeuint;
  528. begin
  529. writeln('Testing exception handling');
  530. mem:=0;
  531. DoMem(mem);
  532. i:=-1;
  533. try
  534. test1;
  535. finally
  536. inc(i);
  537. end;
  538. if i<>2 then
  539. do_error(1001);
  540. i:=-1;
  541. try
  542. test2;
  543. except
  544. inc(i);
  545. end;
  546. if i<>2 then
  547. do_error(1002);
  548. i:=-1;
  549. try
  550. test3;
  551. except
  552. inc(i);
  553. end;
  554. if i<>3 then
  555. do_error(1003);
  556. i:=-1;
  557. test4;
  558. if i<>2 then
  559. do_error(1004);
  560. i:=-1;
  561. test5;
  562. if i<>1 then
  563. do_error(1005);
  564. i:=-1;
  565. test6;
  566. if i<>10 then
  567. do_error(1006);
  568. i:=-1;
  569. test7;
  570. if i<>2 then
  571. do_error(1007);
  572. i:=-1;
  573. test8;
  574. if i<>20 then
  575. do_error(1008);
  576. i:=-1;
  577. test9;
  578. if i<>2 then
  579. do_error(1009);
  580. i:=-1;
  581. test10;
  582. if i<>2 then
  583. do_error(1010);
  584. i:=-1;
  585. test100;
  586. if i<>1 then
  587. do_error(1100);
  588. i:=-1;
  589. test101;
  590. if i<>2 then
  591. do_error(1101);
  592. i:=-1;
  593. test102;
  594. if i<>2 then
  595. do_error(1102);
  596. i:=-1;
  597. test103;
  598. if i<>100 then
  599. do_error(1103);
  600. i:=-1;
  601. test104;
  602. if i<>1 then
  603. do_error(1104);
  604. i:=-1;
  605. test105;
  606. if i<>1 then
  607. do_error(1105);
  608. i:=-1;
  609. test106;
  610. if i<>1 then
  611. do_error(1106);
  612. i:=-1;
  613. test107;
  614. if i<>1 then
  615. do_error(1107);
  616. i:=-1;
  617. test108;
  618. if i<>2 then
  619. do_error(1108);
  620. i:=-1;
  621. test109;
  622. if i<>2 then
  623. do_error(1109);
  624. i:=-1;
  625. test110;
  626. if i<>1 then
  627. do_error(1110);
  628. i:=-1;
  629. test111;
  630. if i<>2 then
  631. do_error(1111);
  632. i:=-1;
  633. test112;
  634. if i<>2 then
  635. do_error(1112);
  636. i:=-1;
  637. test113;
  638. if i<>100 then
  639. do_error(1113);
  640. i:=-1;
  641. test114;
  642. if i<>1 then
  643. do_error(1114);
  644. i:=-1;
  645. test115;
  646. if i<>1 then
  647. do_error(1115);
  648. i:=-1;
  649. test116;
  650. if i<>1 then
  651. do_error(1116);
  652. i:=-1;
  653. test117;
  654. if i<>1 then
  655. do_error(1117);
  656. i:=-1;
  657. test118;
  658. if i<>2 then
  659. do_error(1118);
  660. i:=-1;
  661. test119;
  662. if i<>2 then
  663. do_error(1119);
  664. if DoMem(mem)<>0 then
  665. begin
  666. writeln('exception generates memory holes');
  667. do_error(99999);
  668. end;
  669. writeln('Test successfully passed');
  670. halt(0);
  671. end.