fork download
  1. using System;
  2.  
  3. namespace Dispose_Throw
  4. {
  5. public class Disposable : IDisposable
  6. {
  7. private int n;
  8. public Disposable(int number)
  9. {
  10. n=number;
  11. Console.WriteLine("CTOR, n="+n);
  12. }
  13.  
  14. public void Dispose()
  15. {
  16. Console.WriteLine("Dispose, n="+n);
  17. throw new System.InvalidOperationException("Exception, n=" + n);
  18. }
  19. }
  20. class Program
  21. {
  22. static void Main(string[] args)
  23. {
  24. try
  25. {
  26. using
  27. (
  28. Disposable d1 = new Disposable(1),
  29. d2 = new Disposable(2)
  30. )
  31. {
  32. Console.WriteLine("Body");
  33. }
  34. }
  35. catch (System.InvalidOperationException e)
  36. {
  37. Console.WriteLine(e.Message);
  38. }
  39. Console.WriteLine("End");
  40. }
  41. }
  42. }
  43.  
Success #stdin #stdout 0.04s 36968KB
stdin
Standard input is empty
stdout
CTOR, n=1
CTOR, n=2
Body
Dispose, n=2
Dispose, n=1
Exception, n=1
End