testexc.pp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. program testexceptions;
  2. Type
  3. TAObject = class(TObject)
  4. a : longint;
  5. end;
  6. TBObject = Class(TObject)
  7. b : longint;
  8. end;
  9. Procedure raiseanexception;
  10. Var A : TAObject;
  11. begin
  12. Writeln ('Creating exception object');
  13. A:=TAObject.Create;
  14. Writeln ('Raising with this object');
  15. raise A;
  16. Writeln ('This can''t happen');
  17. end;
  18. Var MaxLevel : longint;
  19. Procedure DoTryFinally (Level : Longint; DoRaise : Boolean);
  20. Var Raised,Reraised : Boolean;
  21. I : Longint;
  22. begin
  23. Try
  24. writeln ('Try(',level,') : Checking for exception');
  25. If Level=MaxLevel then
  26. begin
  27. if DoRaise then
  28. begin
  29. Writeln ('Try(',level,'): Level ',maxlevel,' reached, raising exception.');
  30. Raiseanexception
  31. end
  32. else
  33. Writeln ('Try(',Level,'): Not raising exception')
  34. end
  35. else
  36. begin
  37. Writeln ('Try(',level,') : jumping to next level');
  38. DoTryFinally(Level+1,DoRaise);
  39. end;
  40. finally
  41. Writeln ('Finally (',level,'): Starting code.');
  42. end;
  43. writeln ('Out of try/finally at level (',level,')');
  44. end;
  45. Procedure DoTryExcept (Level : Longint; DoRaise : Boolean);
  46. Var Raised : Boolean;
  47. I : Longint;
  48. Caught : TObject;
  49. begin
  50. Try
  51. writeln ('Try(',level,') : Checking for exception');
  52. If Level=MaxLevel then
  53. if DoRaise then
  54. begin
  55. Writeln ('Try(',level,'): Level ',maxlevel,', raising exception.');
  56. Raiseanexception
  57. end
  58. else
  59. Writeln ('Try(',Level,'): level ',maxlevel,'. Not raising exception')
  60. else
  61. begin
  62. Writeln ('Try(',level,') : jumping to next level');
  63. DoTryExcept(Level+1,DoRaise);
  64. end;
  65. except
  66. On TAObject do Writeln ('Exception was caught by TAObject');
  67. On TBobject do Writeln ('Exception was caught by TBObject');
  68. On E : TObject do Writeln ('Caught object ',E.ClassName);
  69. // writeln ('Except (',level,') : Exception caught by default handler');
  70. end;
  71. writeln ('Out of try/except at level (',level,')');
  72. end;
  73. Procedure DoMix (Level : Longint; DoRaise : Boolean);
  74. Var Raised : Boolean;
  75. I : Longint;
  76. Caught : TObject;
  77. begin
  78. Try
  79. Try
  80. writeln ('Try(',level,') : Checking for exception');
  81. If Level=MaxLevel then
  82. if DoRaise then
  83. begin
  84. Writeln ('Try(',level,'): Level ',maxlevel,', raising exception.');
  85. Raiseanexception
  86. end
  87. else
  88. Writeln ('Try(',Level,'): level ',maxlevel,'. Not raising exception')
  89. else
  90. begin
  91. Writeln ('Try(',level,') : jumping to next level');
  92. DoMix(Level+1,DoRaise);
  93. end;
  94. finally
  95. Writeln ('Mix:Finally (',level,'): Starting code.');
  96. end;
  97. Writeln ('Level (',level,') : Out of try/finally');
  98. except
  99. On TAObject do Writeln ('Exception was caught by TAObject');
  100. On TBobject do Writeln ('Exception was caught by TBObject');
  101. On TObject do writeln ('Except (',level,') : Exception caught by TObject');
  102. // The following don't work...
  103. On E : TObject do Writeln ('Caught object ',E.ClassName);
  104. else
  105. writeln ('Except (',level,') : Exception caught by default handler');
  106. end;
  107. writeln ('Out of try/except at level (',level,')');
  108. end;
  109. function _dotryfinally : boolean;
  110. var
  111. problem : boolean;
  112. begin
  113. result:=false;
  114. try
  115. try
  116. finally
  117. writeln('Raising an exception in finally statement');
  118. Raiseanexception
  119. end;
  120. except
  121. end;
  122. try
  123. exit;
  124. finally
  125. result:=true;
  126. end;
  127. writeln('Problem with finally and exit !!!!');
  128. halt(1);
  129. end;
  130. procedure dotryfinally;
  131. begin
  132. if not(_dotryfinally) then
  133. begin
  134. writeln('Problem with finally and exit !!!!');
  135. halt(1);
  136. end;
  137. end;
  138. Procedure Start(Const Msg : string);
  139. begin
  140. Writeln (Msg);
  141. Writeln;
  142. end;
  143. Procedure Finish;
  144. begin
  145. Writeln;
  146. Write ('Finished. Press enter to continue.');
  147. Readln;
  148. end;
  149. begin
  150. Maxlevel:=3;
  151. Start ('Testing Try/Finally without raise');
  152. DoTryFinally (1,False);
  153. Finish;
  154. Start ('Testing Try/except without raise');
  155. DoTryExcept (1,FAlse);
  156. Finish;
  157. Start ('Testing Mix without raise');
  158. DoMix (1,False);
  159. Finish;
  160. Start ('Testing Try/except with raise');
  161. DoTryExcept (1,true);
  162. Finish;
  163. Start ('Testing Mix with raise');
  164. DoMix (1,true);
  165. Finish;
  166. Start ('Testing Try/Finally with Exit');
  167. dotryfinally;
  168. Finish;
  169. Writeln ('Testing Try/Finally with raise');
  170. Start ('This one should end with an error message !!.');
  171. DoTryFinally (1,True);
  172. end.