#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.


About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #916 – Exception Can Cross .NET Language Boundaries

  1. Pingback: Dew Drop – August 26, 2013 (#1,611) | Alvin Ashcraft's Morning Dew

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: