ttryfin2.pp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. { %RESULT=217 }
  2. {****************************************************************}
  3. { CODE GENERATOR TEST PROGRAM }
  4. { By Carl Eric Codere }
  5. {****************************************************************}
  6. { NODE TESTED : secondtryfinally() }
  7. { secondraise() }
  8. {****************************************************************}
  9. { PRE-REQUISITES: secondload() }
  10. { secondassign() }
  11. { secondtypeconv() }
  12. { secondtryexcept() }
  13. { secondcalln() }
  14. { secondadd() }
  15. {****************************************************************}
  16. { DEFINES: }
  17. { FPC = Target is FreePascal compiler }
  18. {****************************************************************}
  19. {****************************************************************}
  20. program ttryfin2;
  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. end;
  31. { The test cases were taken from the SAL internal architecture manual }
  32. procedure fail;
  33. begin
  34. WriteLn('Failure.');
  35. halt(1);
  36. end;
  37. var
  38. global_counter : integer;
  39. Procedure raiseanexception;
  40. Var A : TAObject;
  41. begin
  42. { Writeln ('Creating exception object');}
  43. A:=TAObject.Create;
  44. { Writeln ('Raising with this object');}
  45. raise A;
  46. { this should never happen, if it does there is a problem! }
  47. RunError(255);
  48. end;
  49. procedure IncrementCounter(x: integer);
  50. begin
  51. Inc(global_counter);
  52. end;
  53. procedure DecrementCounter(x: integer);
  54. begin
  55. Dec(global_counter);
  56. end;
  57. { Will the finally clause of a try block be called if the try block raises an exception? }
  58. Procedure DoTryFinallyOne;
  59. var
  60. failed : boolean;
  61. begin
  62. Write('Try..Finally with exception rise...');
  63. global_counter:=0;
  64. failed:=true;
  65. Try
  66. IncrementCounter(global_counter);
  67. RaiseAnException;
  68. DecrementCounter(global_counter);
  69. finally
  70. if global_counter = 1 then
  71. failed :=false;
  72. if failed then
  73. fail
  74. else
  75. WriteLn('Success!');
  76. end;
  77. end;
  78. Begin
  79. DoTryFinallyOne;
  80. end.