texception4.pp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. {$mode objfpc}
  2. uses
  3. sysutils;
  4. const
  5. Program_has_errors : boolean = false;
  6. exception_called : boolean = false;
  7. TestNumber : longint = 10000;
  8. procedure test_exception(const s : string);
  9. begin
  10. if not(exception_called) then
  11. begin
  12. Writeln('Exception not called : ',s);
  13. Program_has_errors := true;
  14. end;
  15. end;
  16. var
  17. i,j : longint;
  18. e : extended;
  19. exception_count,level : longint;
  20. begin
  21. j:=0;
  22. i:=100;
  23. try
  24. exception_called:=false;
  25. j := i div j;
  26. except
  27. on e : exception do
  28. begin
  29. Writeln('First integer exception called ',e.message);
  30. exception_called:=true;
  31. end;
  32. end;
  33. test_exception('First division by zero for integers');
  34. try
  35. exception_called:=false;
  36. j := i div j;
  37. except
  38. on e : exception do
  39. begin
  40. Writeln('Second integer exception called ',e.message);
  41. exception_called:=true;
  42. end;
  43. end;
  44. test_exception('Second division by zero for integers');
  45. try
  46. exception_called:=false;
  47. e:=i/j;
  48. except
  49. on e : exception do
  50. begin
  51. Writeln('First real exception called ',e.message);
  52. exception_called:=true;
  53. end;
  54. end;
  55. test_exception('First division by zero for reals');
  56. try
  57. exception_called:=false;
  58. e:=i/j;
  59. except
  60. on e : exception do
  61. begin
  62. Writeln('Second real exception called ',e.message);
  63. exception_called:=true;
  64. end;
  65. end;
  66. test_exception('Second division by zero for reals');
  67. try
  68. exception_called:=false;
  69. j := i div j;
  70. except
  71. on e : exception do
  72. begin
  73. Writeln('exception called ',e.message);
  74. exception_called:=true;
  75. end;
  76. end;
  77. test_exception('third division by zero for integers');
  78. exception_count:=0;
  79. level:=0;
  80. for j:=1 to TestNumber do
  81. begin
  82. try
  83. i:=0;
  84. inc(level);
  85. e:=j/i;
  86. except
  87. on e : exception do
  88. begin
  89. inc(exception_count);
  90. if level>1 then
  91. Writeln('exception overrun');
  92. dec(level);
  93. end;
  94. end;
  95. end;
  96. if exception_count<>TestNumber then
  97. begin
  98. program_has_errors:=true;
  99. Writeln('Could not generate ',TestNumber,' consecutive exceptions');
  100. Writeln('Only ',exception_count,' exceptions were generated');
  101. end
  102. else
  103. begin
  104. Writeln(TestNumber,' consecutive exceptions generated successfully');
  105. end;
  106. try
  107. exception_called:=false;
  108. i := -1;
  109. e := ln(i);
  110. except
  111. on e : exception do
  112. begin
  113. Writeln('exception called ',e.message);
  114. exception_called:=true;
  115. end;
  116. end;
  117. test_exception('ln(-1)');
  118. if program_has_errors then
  119. Halt(1);
  120. end.