#916 – Exception Can Cross .NET Language Boundaries

An exception being thrown from one assembly can be caught in a different assembly.  This is true even when the assemblies are authored in different .NET languages.

Below, code in the ConsoleApplication2 assembly, which is written in Visual Basic, calls code in the DogLibrary assembly (the Dog.Bark method), which is written in C#.  When an exception originates in DogLibrarywe can catch it in the code running in ConsoleApplication2.


Assume that the code in DogLibrary looks like:

        public void Bark(int numTimes)
            if (numTimes > 10)
                throw new ApplicationException("Too many times to bark");

            for (int i = 0; i < numTimes; i++)

And the code in ConsoleApplication2 looks like:

    Sub Main()
            Dim d As Dog = New Dog("Bowser")

        Catch ex As Exception
            Console.WriteLine(String.Format("Uh-oh! {0}", ex.ToString()))
        End Try
    End Sub

We can catch the exception in the VB code that was thrown by the C# code.