texception5.pp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. { %version=1.1}
  2. {$IFDEF FPC}
  3. {$MODE OBJFPC}
  4. {$ENDIF}
  5. program texception5;
  6. uses
  7. SysUtils;
  8. type
  9. ETestException = class(Exception)
  10. constructor Create;
  11. destructor Destroy; override;
  12. end;
  13. var
  14. exc_destroyed: boolean;
  15. exc : ETestException;
  16. constructor ETestException.Create;
  17. begin
  18. exc_destroyed := false;
  19. exc := Self;
  20. end;
  21. destructor ETestException.Destroy;
  22. begin
  23. inherited;
  24. exc_destroyed := true;
  25. end;
  26. var
  27. exc2: Exception;
  28. begin
  29. // first test, exception should not be freed
  30. try
  31. raise ETestException.Create;
  32. except
  33. exc2 := Exception(AcquireExceptionObject);
  34. if exc <> exc2 then halt(11);
  35. end;
  36. if exc_destroyed then halt(12);
  37. if exc <> exc2 then halt(13);
  38. exc2.Free;
  39. // second test, exception should be freed
  40. try
  41. raise ETestException.Create;
  42. except
  43. exc2 := Exception(AcquireExceptionObject);
  44. if exc <> exc2 then halt(21);
  45. ReleaseExceptionObject;
  46. end;
  47. if not exc_destroyed then halt(22);
  48. // third test, exception should not be freed
  49. try
  50. raise ETestException.Create;
  51. except
  52. AcquireExceptionObject;
  53. AcquireExceptionObject;
  54. ReleaseExceptionObject;
  55. end;
  56. if exc_destroyed then halt(31);
  57. // exception should be freed
  58. try
  59. raise ETestException.Create;
  60. except
  61. AcquireExceptionObject;
  62. AcquireExceptionObject;
  63. ReleaseExceptionObject;
  64. ReleaseExceptionObject;
  65. end;
  66. if not exc_destroyed then halt(41);
  67. // exception should be freed, refcount zeroed when re-raising
  68. try
  69. try
  70. raise ETestException.Create;
  71. except
  72. on e: exception do begin
  73. AcquireExceptionObject;
  74. raise;
  75. end;
  76. end;
  77. except
  78. end;
  79. if not exc_destroyed then halt(51);
  80. // same as before but without explicit block
  81. try
  82. try
  83. raise ETestException.Create;
  84. except
  85. AcquireExceptionObject;
  86. raise;
  87. end;
  88. except
  89. end;
  90. if not exc_destroyed then halt(61);
  91. end.