ttryexc1.pp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. {****************************************************************}
  2. { CODE GENERATOR TEST PROGRAM }
  3. { By Carl Eric Codere }
  4. {****************************************************************}
  5. { NODE TESTED : secondtryexcept() }
  6. { secondraise() }
  7. {****************************************************************}
  8. { PRE-REQUISITES: secondload() }
  9. { secondassign() }
  10. { secondtypeconv() }
  11. { secondtryexcept() }
  12. { secondcalln() }
  13. { secondadd() }
  14. {****************************************************************}
  15. { DEFINES: }
  16. { FPC = Target is FreePascal compiler }
  17. {****************************************************************}
  18. { REMARKS : Tested with Delphi 3 as reference implementation }
  19. {****************************************************************}
  20. program ttryexc1;
  21. {$ifdef fpc}
  22. {$mode objfpc}
  23. {$endif}
  24. Type
  25. TAObject = class(TObject)
  26. a : longint;
  27. end;
  28. TBObject = Class(TObject)
  29. b : longint;
  30. constructor create(c: longint);
  31. end;
  32. { The test cases were taken from the SAL internal architecture manual }
  33. procedure fail;
  34. begin
  35. WriteLn('Failure.');
  36. halt(1);
  37. end;
  38. var
  39. global_counter : integer;
  40. constructor tbobject.create(c:longint);
  41. begin
  42. inherited create;
  43. b:=c;
  44. end;
  45. Procedure raiseanexception;
  46. Var A : TAObject;
  47. var B : TAobject;
  48. begin
  49. { Writeln ('Creating exception object');}
  50. A:=TAObject.Create;
  51. { Writeln ('Raising with this object');}
  52. raise A;
  53. { this should never happen, if it does there is a problem! }
  54. RunError(255);
  55. end;
  56. procedure IncrementCounter(x: integer);
  57. begin
  58. Inc(global_counter);
  59. end;
  60. procedure DecrementCounter(x: integer);
  61. begin
  62. Dec(global_counter);
  63. end;
  64. Function DoTryExceptOne: boolean;
  65. var
  66. failed : boolean;
  67. begin
  68. Write('Try..Except clause...');
  69. global_counter:=0;
  70. failed:=true;
  71. DoTryExceptOne := failed;
  72. Try
  73. IncrementCounter(global_counter);
  74. DecrementCounter(global_counter);
  75. except
  76. end;
  77. if global_counter = 0 then
  78. failed :=false;
  79. DoTryExceptOne := failed;
  80. end;
  81. Function DoTryExceptTwo : boolean;
  82. var
  83. failed : boolean;
  84. begin
  85. Write('Try..Except with break statement...');
  86. global_counter:=0;
  87. failed:=true;
  88. DoTryExceptTwo := failed;
  89. while (failed) do
  90. begin
  91. Try
  92. IncrementCounter(global_counter);
  93. DecrementCounter(global_counter);
  94. break;
  95. except
  96. end;
  97. end;
  98. if global_counter = 0 then
  99. failed :=false;
  100. DoTryExceptTwo := failed;
  101. end;
  102. Function DoTryExceptFour: boolean;
  103. var
  104. failed : boolean;
  105. begin
  106. Write('Try..Except with exit statement...');
  107. global_counter:=0;
  108. failed:=true;
  109. DoTryExceptFour := failed;
  110. while (failed) do
  111. begin
  112. Try
  113. IncrementCounter(global_counter);
  114. DecrementCounter(global_counter);
  115. DoTryExceptFour := false;
  116. exit;
  117. except
  118. end;
  119. end;
  120. end;
  121. Function DoTryExceptFive: boolean;
  122. var
  123. failed : boolean;
  124. x : integer;
  125. begin
  126. Write('Try..Except nested clauses (three-level nesting)...');
  127. global_counter:=0;
  128. failed:=true;
  129. DoTryExceptFive := failed;
  130. x:=0;
  131. Try
  132. IncrementCounter(global_counter);
  133. Try
  134. DecrementCounter(global_counter);
  135. IncrementCounter(global_counter);
  136. Try
  137. DecrementCounter(global_counter);
  138. except
  139. Inc(x);
  140. end;
  141. except
  142. Inc(x);
  143. End;
  144. except
  145. end;
  146. if (global_counter = 0) then
  147. failed :=false;
  148. DoTryExceptFive := failed;
  149. end;
  150. Function DoTryExceptSix : boolean;
  151. var
  152. failed : boolean;
  153. x: integer;
  154. begin
  155. Write('Try..Except nested clauses with break statement...');
  156. global_counter:=0;
  157. x:=0;
  158. failed:=true;
  159. DoTryExceptSix := failed;
  160. while (failed) do
  161. begin
  162. Try
  163. IncrementCounter(global_counter);
  164. Try
  165. DecrementCounter(global_counter);
  166. IncrementCounter(global_counter);
  167. Try
  168. DecrementCounter(global_counter);
  169. break;
  170. except
  171. Inc(x);
  172. end;
  173. except
  174. Inc(x);
  175. End;
  176. except
  177. end;
  178. end;
  179. if (global_counter = 0) then
  180. failed :=false;
  181. DoTryExceptSix := failed;
  182. end;
  183. Function DoTryExceptEight : boolean;
  184. var
  185. failed : boolean;
  186. x: integer;
  187. begin
  188. Write('Try..Except nested clauses with exit statement...');
  189. global_counter:=0;
  190. x:=0;
  191. failed:=true;
  192. DoTryExceptEight := failed;
  193. while (failed) do
  194. begin
  195. Try
  196. IncrementCounter(global_counter);
  197. Try
  198. DecrementCounter(global_counter);
  199. IncrementCounter(global_counter);
  200. Try
  201. DecrementCounter(global_counter);
  202. DoTryExceptEight := false;
  203. exit;
  204. except
  205. Inc(x);
  206. end;
  207. except
  208. Inc(x);
  209. End;
  210. except
  211. end;
  212. end;
  213. end;
  214. Function DoTryExceptNine : boolean;
  215. var
  216. failed : boolean;
  217. x: integer;
  218. begin
  219. Write('Try..Except nested clauses with break statement in other try-block...');
  220. global_counter:=0;
  221. x:=0;
  222. failed:=true;
  223. DoTryExceptNine := failed;
  224. Try
  225. while (failed) do
  226. begin
  227. Try
  228. IncrementCounter(global_counter);
  229. Try
  230. DecrementCounter(global_counter);
  231. IncrementCounter(global_counter);
  232. Try
  233. DecrementCounter(global_counter);
  234. break;
  235. except
  236. Inc(x);
  237. end;
  238. except
  239. Inc(x);
  240. End;
  241. except
  242. end;
  243. end; {end while }
  244. except
  245. { normally this should execute! }
  246. DoTryExceptNine := failed;
  247. end;
  248. if (global_counter = 0) and (x = 0) then
  249. failed :=false;
  250. DoTryExceptNine := failed;
  251. end;
  252. {****************************************************************************}
  253. {***************************************************************************}
  254. { Exception is thrown }
  255. {***************************************************************************}
  256. Function DoTryExceptTen: boolean;
  257. var
  258. failed : boolean;
  259. begin
  260. Write('Try..Except clause with raise...');
  261. global_counter:=0;
  262. failed:=true;
  263. DoTryExceptTen := failed;
  264. Try
  265. IncrementCounter(global_counter);
  266. RaiseAnException;
  267. DecrementCounter(global_counter);
  268. except
  269. if global_counter = 1 then
  270. failed :=false;
  271. DoTryExceptTen := failed;
  272. end;
  273. end;
  274. Function DoTryExceptEleven : boolean;
  275. var
  276. failed : boolean;
  277. begin
  278. Write('Try..Except with raise and break statement...');
  279. global_counter:=0;
  280. failed:=true;
  281. DoTryExceptEleven := failed;
  282. while (failed) do
  283. begin
  284. Try
  285. IncrementCounter(global_counter);
  286. DecrementCounter(global_counter);
  287. RaiseAnException;
  288. break;
  289. except
  290. if global_counter = 0 then
  291. failed :=false;
  292. DoTryExceptEleven := failed;
  293. end;
  294. end;
  295. end;
  296. Function DoTryExceptTwelve: boolean;
  297. var
  298. failed : boolean;
  299. x : integer;
  300. begin
  301. Write('Try..Except nested clauses (three-level nesting)...');
  302. global_counter:=0;
  303. failed:=true;
  304. DoTryExceptTwelve := failed;
  305. x:=0;
  306. Try
  307. IncrementCounter(global_counter);
  308. Try
  309. DecrementCounter(global_counter);
  310. IncrementCounter(global_counter);
  311. Try
  312. DecrementCounter(global_counter);
  313. RaiseAnException;
  314. except
  315. if (global_counter = 0) then
  316. failed :=false;
  317. DoTryExceptTwelve := failed;
  318. end;
  319. except
  320. DoTryExceptTwelve := true;
  321. End;
  322. except
  323. DoTryExceptTwelve := true;
  324. end;
  325. end;
  326. Function DoTryExceptThirteen: boolean;
  327. var
  328. failed : boolean;
  329. x : integer;
  330. begin
  331. Write('Try..Except nested clauses (three-level nesting)...');
  332. global_counter:=0;
  333. failed:=true;
  334. DoTryExceptThirteen := failed;
  335. x:=0;
  336. Try
  337. IncrementCounter(global_counter);
  338. Try
  339. DecrementCounter(global_counter);
  340. IncrementCounter(global_counter);
  341. RaiseAnException;
  342. Try
  343. DecrementCounter(global_counter);
  344. except
  345. DoTryExceptThirteen := true;
  346. end;
  347. except
  348. if (global_counter = 1) then
  349. failed :=false;
  350. DoTryExceptThirteen := failed;
  351. End;
  352. except
  353. DoTryExceptThirteen := true;
  354. end;
  355. end;
  356. {***************************************************************************}
  357. { Exception is thrown in except block }
  358. {***************************************************************************}
  359. Function DoTryExceptFourteen: boolean;
  360. var
  361. failed : boolean;
  362. x : integer;
  363. begin
  364. Write('Try..Except nested clauses with single re-raise...');
  365. global_counter:=0;
  366. failed:=true;
  367. DoTryExceptFourteen := failed;
  368. x:=0;
  369. Try
  370. IncrementCounter(global_counter);
  371. Try
  372. DecrementCounter(global_counter);
  373. IncrementCounter(global_counter);
  374. Try
  375. DecrementCounter(global_counter);
  376. RaiseAnException;
  377. except
  378. { raise to next block }
  379. Raise;
  380. end;
  381. except
  382. if (global_counter = 0) then
  383. failed :=false;
  384. DoTryExceptFourteen := failed;
  385. End;
  386. except
  387. DoTryExceptFourteen := true;
  388. end;
  389. end;
  390. Function DoTryExceptFifteen: boolean;
  391. var
  392. failed : boolean;
  393. x : integer;
  394. begin
  395. Write('Try..Except nested clauses with re-reraises (1)...');
  396. global_counter:=0;
  397. failed:=true;
  398. DoTryExceptFifteen := failed;
  399. x:=0;
  400. Try
  401. IncrementCounter(global_counter);
  402. Try
  403. DecrementCounter(global_counter);
  404. IncrementCounter(global_counter);
  405. Try
  406. DecrementCounter(global_counter);
  407. RaiseAnException;
  408. except
  409. { raise to next block }
  410. Raise;
  411. end;
  412. except
  413. { re-raise to next block }
  414. Raise;
  415. End;
  416. except
  417. if (global_counter = 0) then
  418. failed :=false;
  419. DoTryExceptFifteen := failed;
  420. end;
  421. end;
  422. procedure nestedtryblock(var global_counter: integer);
  423. begin
  424. IncrementCounter(global_counter);
  425. Try
  426. DecrementCounter(global_counter);
  427. IncrementCounter(global_counter);
  428. Try
  429. DecrementCounter(global_counter);
  430. RaiseAnException;
  431. except
  432. { raise to next block }
  433. Raise;
  434. end;
  435. except
  436. { re-raise to next block }
  437. Raise;
  438. End;
  439. end;
  440. Function DoTryExceptSixteen: boolean;
  441. var
  442. failed : boolean;
  443. x : integer;
  444. begin
  445. Write('Try..Except nested clauses with re-reraises (2)...');
  446. global_counter:=0;
  447. failed:=true;
  448. DoTryExceptSixteen := failed;
  449. x:=0;
  450. Try
  451. NestedTryBlock(global_counter);
  452. except
  453. if (global_counter = 0) then
  454. failed :=false;
  455. DoTryExceptSixteen := failed;
  456. end;
  457. end;
  458. Function DoTryExceptSeventeen: boolean;
  459. var
  460. failed : boolean;
  461. x : integer;
  462. begin
  463. Write('Try..Except nested clauses with raises...');
  464. global_counter:=0;
  465. failed:=true;
  466. DoTryExceptSeventeen := failed;
  467. x:=0;
  468. Try
  469. IncrementCounter(global_counter);
  470. Try
  471. DecrementCounter(global_counter);
  472. IncrementCounter(global_counter);
  473. Try
  474. DecrementCounter(global_counter);
  475. RaiseAnException;
  476. except
  477. { raise to next block }
  478. raise TAObject.Create;
  479. end;
  480. except
  481. { re-raise to next block }
  482. raise TBObject.Create(1234);
  483. End;
  484. except
  485. if (global_counter = 0) then
  486. failed :=false;
  487. DoTryExceptSeventeen := failed;
  488. end;
  489. end;
  490. {***************************************************************************}
  491. { Exception flow control in except block }
  492. {***************************************************************************}
  493. Function DoTryExceptEighteen: boolean;
  494. var
  495. failed : boolean;
  496. begin
  497. Write('Try..Except clause with raise with break in except block...');
  498. global_counter:=0;
  499. failed:=true;
  500. DoTryExceptEighteen := failed;
  501. while (failed) do
  502. begin
  503. Try
  504. IncrementCounter(global_counter);
  505. RaiseAnException;
  506. DecrementCounter(global_counter);
  507. except
  508. if global_counter = 1 then
  509. failed :=false;
  510. DoTryExceptEighteen := failed;
  511. break;
  512. end;
  513. end;
  514. end;
  515. Function DoTryExceptNineteen: boolean;
  516. var
  517. failed : boolean;
  518. begin
  519. Write('Try..Except clause with raise with exit in except block...');
  520. global_counter:=0;
  521. failed:=true;
  522. DoTryExceptNineteen := failed;
  523. while (failed) do
  524. begin
  525. Try
  526. IncrementCounter(global_counter);
  527. RaiseAnException;
  528. DecrementCounter(global_counter);
  529. except
  530. if global_counter = 1 then
  531. failed :=false;
  532. DoTryExceptNineteen := failed;
  533. exit;
  534. end;
  535. end;
  536. end;
  537. Function DoTryExceptTwenty: boolean;
  538. var
  539. failed : boolean;
  540. x : integer;
  541. begin
  542. Write('Try..Except nested clauses with raises with break in inner try...');
  543. global_counter:=0;
  544. failed:=true;
  545. DoTryExceptTwenty := failed;
  546. x:=0;
  547. Try
  548. IncrementCounter(global_counter);
  549. Try
  550. while (x = 0) do
  551. begin
  552. DecrementCounter(global_counter);
  553. IncrementCounter(global_counter);
  554. Try
  555. DecrementCounter(global_counter);
  556. RaiseAnException;
  557. except
  558. { raise to next block }
  559. raise TAObject.Create;
  560. break;
  561. end;
  562. end;
  563. except
  564. { re-raise to next block }
  565. raise TBObject.Create(1234);
  566. End;
  567. except
  568. if (global_counter = 0) then
  569. failed :=false;
  570. DoTryExceptTwenty := failed;
  571. end;
  572. end;
  573. Function DoTryExceptTwentyOne: boolean;
  574. var
  575. failed : boolean;
  576. x : integer;
  577. begin
  578. Write('Try..Except nested clauses with raises with continue in inner try...');
  579. global_counter:=0;
  580. failed:=true;
  581. DoTryExceptTwentyOne := failed;
  582. x:=0;
  583. Try
  584. IncrementCounter(global_counter);
  585. Try
  586. while (x = 0) do
  587. begin
  588. DecrementCounter(global_counter);
  589. IncrementCounter(global_counter);
  590. Try
  591. DecrementCounter(global_counter);
  592. RaiseAnException;
  593. except
  594. { raise to next block }
  595. raise TAObject.Create;
  596. x:=1;
  597. continue;
  598. end;
  599. end;
  600. except
  601. { re-raise to next block }
  602. raise TBObject.Create(1234);
  603. End;
  604. except
  605. if (global_counter = 0) then
  606. failed :=false;
  607. DoTryExceptTwentyOne := failed;
  608. end;
  609. end;
  610. Function DoTryExceptTwentyTwo: boolean;
  611. var
  612. failed : boolean;
  613. x : integer;
  614. begin
  615. Write('Try..Except nested clauses with raises with exit in inner try...');
  616. global_counter:=0;
  617. failed:=true;
  618. DoTryExceptTwentyTwo := failed;
  619. x:=0;
  620. Try
  621. IncrementCounter(global_counter);
  622. Try
  623. while (x = 0) do
  624. begin
  625. DecrementCounter(global_counter);
  626. IncrementCounter(global_counter);
  627. Try
  628. DecrementCounter(global_counter);
  629. RaiseAnException;
  630. except
  631. { raise to next block }
  632. raise TAObject.Create;
  633. exit;
  634. end;
  635. end;
  636. except
  637. { re-raise to next block }
  638. raise TBObject.Create(1234);
  639. End;
  640. except
  641. if (global_counter = 0) then
  642. failed :=false;
  643. DoTryExceptTwentyTwo := failed;
  644. end;
  645. end;
  646. var
  647. failed: boolean;
  648. begin
  649. failed := DoTryExceptOne;
  650. if failed then
  651. fail
  652. else
  653. WriteLn('Success!');
  654. failed := DoTryExceptTwo;
  655. if failed then
  656. fail
  657. else
  658. WriteLn('Success!');
  659. { failed := DoTryExceptThree;
  660. if failed then
  661. fail
  662. else
  663. WriteLn('Success!');}
  664. failed := DoTryExceptFour;
  665. if failed then
  666. fail
  667. else
  668. WriteLn('Success!');
  669. failed := DoTryExceptFive;
  670. if failed then
  671. fail
  672. else
  673. WriteLn('Success!');
  674. failed := DoTryExceptSix;
  675. if failed then
  676. fail
  677. else
  678. WriteLn('Success!');
  679. { failed := DoTryExceptSeven;
  680. if failed then
  681. fail
  682. else
  683. WriteLn('Success!');}
  684. failed := DoTryExceptEight;
  685. if failed then
  686. fail
  687. else
  688. WriteLn('Success!');
  689. failed := DoTryExceptNine;
  690. if failed then
  691. fail
  692. else
  693. WriteLn('Success!');
  694. (************************ Exceptions are created from here ****************************)
  695. failed := DoTryExceptTen;
  696. if failed then
  697. fail
  698. else
  699. WriteLn('Success!');
  700. failed := DoTryExceptEleven;
  701. if failed then
  702. fail
  703. else
  704. WriteLn('Success!');
  705. failed := DoTryExceptTwelve;
  706. if failed then
  707. fail
  708. else
  709. WriteLn('Success!');
  710. failed := DoTryExceptThirteen;
  711. if failed then
  712. fail
  713. else
  714. WriteLn('Success!');
  715. (************************ Exceptions in except block ****************************)
  716. failed := DoTryExceptFourteen;
  717. if failed then
  718. fail
  719. else
  720. WriteLn('Success!');
  721. failed := DoTryExceptFifteen;
  722. if failed then
  723. fail
  724. else
  725. WriteLn('Success!');
  726. failed := DoTryExceptSixteen;
  727. if failed then
  728. fail
  729. else
  730. WriteLn('Success!');
  731. failed := DoTryExceptSeventeen;
  732. if failed then
  733. fail
  734. else
  735. WriteLn('Success!');
  736. failed := DoTryExceptEighteen;
  737. if failed then
  738. fail
  739. else
  740. WriteLn('Success!');
  741. failed := DoTryExceptNineteen;
  742. if failed then
  743. fail
  744. else
  745. WriteLn('Success!');
  746. failed := DoTryExceptTwenty;
  747. if failed then
  748. fail
  749. else
  750. WriteLn('Success!');
  751. failed := DoTryExceptTwentyOne;
  752. if failed then
  753. fail
  754. else
  755. WriteLn('Success!');
  756. failed := DoTryExceptTwentyTwo;
  757. if failed then
  758. fail
  759. else
  760. WriteLn('Success!');
  761. end.
  762. {
  763. $Log$
  764. Revision 1.2 2002-09-01 14:45:54 peter
  765. * updates to compile with kylix
  766. * fixed some tests
  767. Revision 1.1 2002/08/03 11:05:14 carl
  768. + exception handling testing
  769. (still missing raise / on node testing)
  770. }