' 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) 
﻿Imports System

Namespace E
    Public Class ExceptionB
        Inherits Exception
    End Class
    Public Class ExceptionA
        Inherits Exception
    End Class
    Public Class B
        Implements IDisposable
        Public Sub New()
            Console.WriteLine("B ctor")
        End Sub
        Public Sub DoSomething()
            Throw New ExceptionB()
        End Sub
        Public Sub Dispose() Implements IDisposable.Dispose
            Console.WriteLine("B Close")
        End Sub
    End Class
    Public Class A
        Implements IDisposable
        Public Sub New()
            Console.WriteLine("A ctor")
        End Sub
        Public Sub DoSomething()
        End Sub
        Public Sub DoSomethingElse(ob As B)
            ob.DoSomething()
        End Sub
        Public Sub Dispose() Implements IDisposable.Dispose
            Console.WriteLine("A Close")                  
        End Sub
    End Class
    Public Class Program
        Public Shared Sub Main(args As String())
            Try
                Using oa As New A()
                    oa.DoSomething()
                    Using ob As New B()
                        oa.DoSomethingElse(ob)
                        Console.WriteLine("Everything OK")
                    End Using
                End Using
            Catch exa As ExceptionA
                Console.WriteLine("We have a problem")
            Catch exb As ExceptionB
                Console.WriteLine("We have another problem")
            End Try
        End Sub
    End Class
End Namespace
