ExceptionTest.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //
  2. // ExceptionTest.cs - NUnit Test Cases for the System.Exception class
  3. //
  4. // Linus Upson ([email protected])
  5. //
  6. using System;
  7. using NUnit.Framework;
  8. namespace MonoTests.System
  9. {
  10. public class ExceptionTest : TestCase
  11. {
  12. public ExceptionTest() {}
  13. // This test makes sure that exceptions thrown on block boundaries are
  14. // handled in the correct block. The meaning of the 'caught' variable is
  15. // a little confusing since there are two catchers: the method being
  16. // tested the the method calling the test. There is probably a better
  17. // name, but I can't think of it right now.
  18. public void TestThrowOnBlockBoundaries()
  19. {
  20. bool caught;
  21. try {
  22. caught = false;
  23. ThrowBeforeTry();
  24. } catch {
  25. caught = true;
  26. }
  27. Assert("Exceptions thrown before try blocks should not be caught", caught);
  28. try {
  29. caught = false;
  30. ThrowAtBeginOfTry();
  31. } catch {
  32. caught = true;
  33. }
  34. Assert("Exceptions thrown at begin of try blocks should be caught", !caught);
  35. try {
  36. caught = false;
  37. ThrowAtEndOfTry();
  38. } catch {
  39. caught = true;
  40. }
  41. Assert("Exceptions thrown at end of try blocks should be caught", !caught);
  42. try {
  43. caught = false;
  44. ThrowAtBeginOfCatch();
  45. } catch {
  46. caught = true;
  47. }
  48. Assert("Exceptions thrown at begin of catch blocks should not be caught", caught);
  49. try {
  50. caught = false;
  51. ThrowAtEndOfCatch();
  52. } catch {
  53. caught = true;
  54. }
  55. Assert("Exceptions thrown at end of catch blocks should not be caught", caught);
  56. try {
  57. caught = false;
  58. ThrowAtBeginOfFinally();
  59. } catch {
  60. caught = true;
  61. }
  62. Assert("Exceptions thrown at begin of finally blocks should not be caught", caught);
  63. try {
  64. caught = false;
  65. ThrowAtEndOfFinally();
  66. } catch {
  67. caught = true;
  68. }
  69. Assert("Exceptions thrown at end of finally blocks should not be caught", caught);
  70. try {
  71. caught = false;
  72. ThrowAfterFinally();
  73. } catch {
  74. caught = true;
  75. }
  76. Assert("Exceptions thrown after finally blocks should not be caught", caught);
  77. }
  78. private static void DoNothing()
  79. {
  80. }
  81. private static void ThrowException()
  82. {
  83. throw new Exception();
  84. }
  85. private static void ThrowBeforeTry()
  86. {
  87. ThrowException();
  88. try {
  89. DoNothing();
  90. } catch (Exception) {
  91. DoNothing();
  92. }
  93. }
  94. private static void ThrowAtBeginOfTry()
  95. {
  96. DoNothing();
  97. try {
  98. ThrowException();
  99. DoNothing();
  100. } catch (Exception) {
  101. DoNothing();
  102. }
  103. }
  104. private static void ThrowAtEndOfTry()
  105. {
  106. DoNothing();
  107. try {
  108. DoNothing();
  109. ThrowException();
  110. } catch (Exception) {
  111. DoNothing();
  112. }
  113. }
  114. private static void ThrowAtBeginOfCatch()
  115. {
  116. DoNothing();
  117. try {
  118. DoNothing();
  119. ThrowException();
  120. } catch (Exception) {
  121. throw;
  122. }
  123. }
  124. private static void ThrowAtEndOfCatch()
  125. {
  126. DoNothing();
  127. try {
  128. DoNothing();
  129. ThrowException();
  130. } catch (Exception) {
  131. DoNothing();
  132. throw;
  133. }
  134. }
  135. private static void ThrowAtBeginOfFinally()
  136. {
  137. DoNothing();
  138. try {
  139. DoNothing();
  140. ThrowException();
  141. } catch (Exception) {
  142. DoNothing();
  143. } finally {
  144. ThrowException();
  145. DoNothing();
  146. }
  147. }
  148. private static void ThrowAtEndOfFinally()
  149. {
  150. DoNothing();
  151. try {
  152. DoNothing();
  153. ThrowException();
  154. } catch (Exception) {
  155. DoNothing();
  156. } finally {
  157. DoNothing();
  158. ThrowException();
  159. }
  160. }
  161. private static void ThrowAfterFinally()
  162. {
  163. DoNothing();
  164. try {
  165. DoNothing();
  166. ThrowException();
  167. } catch (Exception) {
  168. DoNothing();
  169. } finally {
  170. DoNothing();
  171. }
  172. ThrowException();
  173. }
  174. }
  175. }