You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

478 lines
8.3 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. // Copyright (c) AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. #if CS60
  20. using System.IO;
  21. #endif
  22. using System.Threading;
  23. using System.Threading.Tasks;
  24. namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
  25. {
  26. public abstract class ExceptionHandling
  27. {
  28. public abstract bool B(int i);
  29. public abstract Task<bool> T();
  30. public abstract void M(int i);
  31. public bool ConditionalReturnInThrow()
  32. {
  33. try
  34. {
  35. if (B(0))
  36. {
  37. return B(1);
  38. }
  39. }
  40. catch
  41. {
  42. }
  43. return false;
  44. }
  45. public bool SimpleTryCatchException()
  46. {
  47. try
  48. {
  49. Console.WriteLine("Try");
  50. return B(new Random().Next());
  51. }
  52. catch (Exception)
  53. {
  54. Console.WriteLine("CatchException");
  55. }
  56. return false;
  57. }
  58. public bool SimpleTryCatchExceptionWithName()
  59. {
  60. try
  61. {
  62. Console.WriteLine("Try");
  63. return B(new Random().Next());
  64. }
  65. catch (Exception ex)
  66. {
  67. Console.WriteLine("CatchException ex: " + ex.ToString());
  68. }
  69. return false;
  70. }
  71. #if CS60
  72. public bool SimpleTryCatchExceptionWithNameAndCondition()
  73. {
  74. try
  75. {
  76. Console.WriteLine("Try");
  77. return B(new Random().Next());
  78. }
  79. catch (Exception ex) when (ex.Message.Contains("test"))
  80. {
  81. Console.WriteLine("CatchException ex: " + ex.ToString());
  82. }
  83. return false;
  84. }
  85. public bool SimpleTryCatchExceptionWithNameAndConditionWithOr()
  86. {
  87. try
  88. {
  89. Console.WriteLine("Try");
  90. return B(new Random().Next());
  91. }
  92. catch (Exception ex) when (ex is ArgumentException || ex is IOException)
  93. {
  94. Console.WriteLine("CatchException ex: " + ex.ToString());
  95. }
  96. return false;
  97. }
  98. public async Task<bool> SimpleAsyncTryCatchExceptionWithNameAndConditionWithOr()
  99. {
  100. try
  101. {
  102. Console.WriteLine("Try");
  103. return await T();
  104. }
  105. catch (Exception ex) when (ex is ArgumentException || ex is IOException)
  106. {
  107. Console.WriteLine("CatchException ex: " + ex.ToString());
  108. }
  109. return false;
  110. }
  111. public void CatchWhenWithConditionWithoutExceptionVar()
  112. {
  113. int num = 0;
  114. try
  115. {
  116. throw new Exception();
  117. }
  118. catch (Exception) when (num == 0)
  119. {
  120. Console.WriteLine("jo");
  121. }
  122. }
  123. #endif
  124. public bool SimpleTryFinally()
  125. {
  126. try
  127. {
  128. Console.WriteLine("Try");
  129. }
  130. finally
  131. {
  132. Console.WriteLine("Finally");
  133. }
  134. return false;
  135. }
  136. public void MethodEndingWithEndFinally()
  137. {
  138. try
  139. {
  140. throw null;
  141. }
  142. finally
  143. {
  144. Console.WriteLine();
  145. }
  146. }
  147. public void MethodEndingWithRethrow()
  148. {
  149. try
  150. {
  151. throw null;
  152. }
  153. catch
  154. {
  155. throw;
  156. }
  157. }
  158. public void TryCatchFinally()
  159. {
  160. try
  161. {
  162. Console.WriteLine("Try");
  163. }
  164. catch (Exception ex)
  165. {
  166. Console.WriteLine(ex.Message);
  167. }
  168. finally
  169. {
  170. Console.WriteLine("Finally");
  171. }
  172. }
  173. public void TryCatchMultipleHandlers()
  174. {
  175. try
  176. {
  177. Console.WriteLine("Try");
  178. }
  179. catch (InvalidOperationException ex)
  180. {
  181. Console.WriteLine(ex.Message);
  182. }
  183. catch (SystemException ex2)
  184. {
  185. Console.WriteLine(ex2.Message);
  186. }
  187. catch
  188. {
  189. Console.WriteLine("other");
  190. }
  191. }
  192. //public void TwoCatchBlocksWithSameVariable()
  193. //{
  194. // try {
  195. // Console.WriteLine("Try1");
  196. // } catch (Exception ex) {
  197. // Console.WriteLine(ex.Message);
  198. // }
  199. // try {
  200. // Console.WriteLine("Try2");
  201. // } catch (Exception ex) {
  202. // Console.WriteLine(ex.Message);
  203. // }
  204. //}
  205. public void NoUsingStatementBecauseTheVariableIsAssignedTo()
  206. {
  207. CancellationTokenSource cancellationTokenSource = null;
  208. try
  209. {
  210. cancellationTokenSource = new CancellationTokenSource();
  211. }
  212. finally
  213. {
  214. if (cancellationTokenSource != null)
  215. {
  216. cancellationTokenSource.Dispose();
  217. }
  218. }
  219. }
  220. public void ThrowInFinally()
  221. {
  222. try
  223. {
  224. }
  225. finally
  226. {
  227. throw new Exception();
  228. }
  229. }
  230. #if ROSLYN || !OPT
  231. // TODO Non-Roslyn compilers create a second while loop inside the try, by inverting the if
  232. // This is fixed in the non-optimised version by the enabling the RemoveDeadCode flag
  233. //public bool EarlyExitInLoopTry()
  234. //{
  235. // while (true) {
  236. // try {
  237. // while (B(0)) {
  238. // Console.WriteLine();
  239. // }
  240. //
  241. // return false;
  242. // } catch {
  243. // }
  244. // }
  245. //}
  246. public bool EarlyExitInLoopTry()
  247. {
  248. while (true)
  249. {
  250. try
  251. {
  252. if (!B(0))
  253. {
  254. return false;
  255. }
  256. Console.WriteLine();
  257. }
  258. catch
  259. {
  260. }
  261. }
  262. }
  263. #endif
  264. public bool ComplexConditionalReturnInThrow()
  265. {
  266. try
  267. {
  268. if (B(0))
  269. {
  270. if (B(1))
  271. {
  272. Console.WriteLine("0 && 1");
  273. return B(2);
  274. }
  275. if (B(3))
  276. {
  277. Console.WriteLine("0 && 3");
  278. return !B(2);
  279. }
  280. Console.WriteLine("0");
  281. }
  282. Console.WriteLine("End Try");
  283. }
  284. catch
  285. {
  286. try
  287. {
  288. try
  289. {
  290. if (((B(0) || B(1)) && B(2)) || B(3))
  291. {
  292. return B(4) && !B(5);
  293. }
  294. if (B(6) || B(7))
  295. {
  296. return B(8) || B(9);
  297. }
  298. }
  299. catch
  300. {
  301. Console.WriteLine("Catch2");
  302. }
  303. return B(10) && B(11);
  304. }
  305. catch
  306. {
  307. Console.WriteLine("Catch");
  308. }
  309. finally
  310. {
  311. Console.WriteLine("Finally");
  312. }
  313. }
  314. return false;
  315. }
  316. public void AppropriateLockExit()
  317. {
  318. int num = 0;
  319. lock (this)
  320. {
  321. if (num <= 256)
  322. {
  323. Console.WriteLine(0);
  324. }
  325. else if (num <= 1024)
  326. {
  327. Console.WriteLine(1);
  328. }
  329. else if (num <= 16384)
  330. {
  331. Console.WriteLine(2);
  332. }
  333. }
  334. }
  335. public void ReassignExceptionVar()
  336. {
  337. try
  338. {
  339. Console.WriteLine("ReassignExceptionVar");
  340. }
  341. catch (Exception innerException)
  342. {
  343. if (innerException.InnerException != null)
  344. {
  345. innerException = innerException.InnerException;
  346. }
  347. Console.WriteLine(innerException);
  348. }
  349. }
  350. public int UseExceptionVarOutsideCatch()
  351. {
  352. Exception ex2;
  353. try
  354. {
  355. return 1;
  356. }
  357. catch (Exception ex)
  358. {
  359. ex2 = ex;
  360. }
  361. Console.WriteLine(ex2 != null);
  362. return 2;
  363. }
  364. public void GenericException<TException>(int input) where TException : Exception
  365. {
  366. try
  367. {
  368. Console.WriteLine(input);
  369. }
  370. catch (TException val)
  371. {
  372. Console.WriteLine(val.Message);
  373. throw;
  374. }
  375. }
  376. public void GenericException2<T>() where T : Exception
  377. {
  378. try
  379. {
  380. Console.WriteLine("CatchT");
  381. #if ROSLYN
  382. }
  383. catch (T val)
  384. {
  385. Console.WriteLine("{0} {1}", val, val.ToString());
  386. }
  387. #else
  388. }
  389. catch (T arg)
  390. {
  391. Console.WriteLine("{0} {1}", arg, arg.ToString());
  392. }
  393. #endif
  394. }
  395. #if CS60
  396. public void GenericExceptionWithCondition<TException>(int input) where TException : Exception
  397. {
  398. try
  399. {
  400. Console.WriteLine(input);
  401. }
  402. catch (TException val) when (val.Message.Contains("Test"))
  403. {
  404. Console.WriteLine(val.Message);
  405. throw;
  406. }
  407. }
  408. public void GenericException2WithCondition<TException>(int input) where TException : Exception
  409. {
  410. try
  411. {
  412. Console.WriteLine(input);
  413. }
  414. catch (TException val) when (val.Message.Contains("Test"))
  415. {
  416. Console.WriteLine("{0} {1}", val, val.ToString());
  417. }
  418. }
  419. public void XXX1()
  420. {
  421. try
  422. {
  423. Console.WriteLine();
  424. }
  425. catch (Exception ex) when (ex.Data.IsFixedSize)
  426. {
  427. Console.WriteLine(ex.ToString());
  428. throw ex;
  429. }
  430. }
  431. public void XXX2()
  432. {
  433. try
  434. {
  435. Console.WriteLine();
  436. }
  437. catch (Exception ex) when (ex is InternalBufferOverflowException)
  438. {
  439. Console.WriteLine(ex.ToString());
  440. throw ex;
  441. }
  442. }
  443. #endif
  444. }
  445. }