// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; namespace E { public class ExceptionB : Exception { } public class ExceptionA : Exception { } public class B : IDisposable { public B() { Console.WriteLine("B ctor"); } public void DoSomething() { throw new ExceptionB(); } public void Dispose() { Console.WriteLine("B Close"); } } public class A : IDisposable { public A() { Console.WriteLine("A ctor"); } public void DoSomething() { } public void DoSomethingElse(B ob) { ob.DoSomething(); } public void Dispose() { Console.WriteLine("A Close"); } } public class Program { public static void Main(string[] args) { try { using(A oa = new A()) { oa.DoSomething(); using(B ob = new B()) { oa.DoSomethingElse(ob); Console.WriteLine("Everything OK"); } } } catch(ExceptionA) { Console.WriteLine("We have a problem"); } catch(ExceptionB) { Console.WriteLine("We have another problem"); } } } }