fork download
  1. using System;
  2.  
  3. class A : IDisposable{
  4. public void Dispose(){
  5. Console.WriteLine("Dispose A");
  6. throw new Exception();
  7. }
  8. }
  9.  
  10. class B : IDisposable{
  11. public void Dispose(){
  12. Console.WriteLine("Dispose B");
  13. }
  14. }
  15.  
  16. class C : IDisposable{
  17. A a = new A();
  18. B b = new B();
  19. public void Dispose(){
  20. a.Dispose();
  21. b.Dispose();
  22. }
  23. }
  24.  
  25. class D : IDisposable{
  26. A a = new A();
  27. B b = new B();
  28. public void Dispose(){
  29. using(a)
  30. using(b)
  31. {}
  32. }
  33. }
  34.  
  35. class E : IDisposable{
  36. A a = new A();
  37. B b = new B();
  38. public void Dispose(){
  39. try{a.Dispose();}finally{
  40. try{b.Dispose();}finally{
  41. }
  42. }
  43. }
  44. }
  45.  
  46. public class Test{
  47. public static void Main(){
  48. try{
  49. using(C c = new C()){
  50. Console.WriteLine("using C");
  51. }
  52. }
  53. catch(Exception e){Console.WriteLine("catch");}
  54.  
  55. Console.WriteLine("------------");
  56.  
  57. try{
  58. using(D d = new D()){
  59. Console.WriteLine("using D");
  60. }
  61. }
  62. catch(Exception e){Console.WriteLine("catch");}
  63.  
  64. Console.WriteLine("------------");
  65.  
  66. try{
  67. using(E e = new E()){
  68. Console.WriteLine("using E");
  69. }
  70. }
  71. catch(Exception e){Console.WriteLine("catch");}
  72. }
  73. }
Success #stdin #stdout 0.03s 37920KB
stdin
Standard input is empty
stdout
using C
Dispose A
catch
------------
using D
Dispose B
Dispose A
catch
------------
using E
Dispose A
Dispose B
catch