texception4.pp 2.8 KB

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