Using Exceptions Across Languages

One of the great things about managed exceptions in C++ is that they work across languages, so now you can, for example, throw an exception in C++ and catch it in a Visual Basic application. No longer are exceptions simply a C++ feature, and this ability to harmonize error handling across code written in different languages makes mixed-language programming much easier than it has been in the past.

In the final example in this chapter, you will create a C++ class in a dynamic link library (DLL) and then use the class in a Visual Basic application.


You will need to have Visual Basic .NET installed to complete the second part of this example.

  1. Start Visual Studio .NET, and open a new Visual C++ project. This time, choose a Class Library (.NET) project, which is used when you want to create a DLL rather than an EXE. I named the project MyClass. You can name it what you like, but make a note of the name.

    You’ll find that you’ve created a project that defines a namespace named MyClass, containing a single class named Class1. It’s this class that you’ll edit, adding a method that can be called from a Visual Basic client.

  2. The project will contain a number of files, among them MyClass.h and MyClass.cpp, which are used to hold the definition and implementation of the Class1 class. Open MyClass.h, and add the Test function so that it looks like the following code:

    // MyClass.h #pragma once using namespace System; namespace MyClass { public __gc class Class1 { public: void Test(int n) { if (n < 0) throw new ArgumentException( S"Argument must be positive"); } }; } 

    The Test method should look familiar by now: it simply checks its argument and throws an exception if it’s less than 0.

  3. Build the project. You’ll end up with a DLL being created in the project’s debug directory.

  4. Close the project, and create a new Visual Basic Console Application project, which I named Tester. Before you can access the C++ DLL you just created, you have to add a reference to it to the project. To do so, open Solution Explorer, expand the project, and right-click the References icon.

  5. Choose Add Reference from the drop-down menu, and when the dialog box appears, click Browse and search for the DLL you built in Step 3. Make sure it’s added to the Selected Components pane, and then click OK.

    click to expand

  6. Now you can add the code to the project. Edit the Main function so that it looks like the following code:

    ‘ Application to demonstrate cross-language exception handling Imports [MyClass] Module Module1 Sub Main() Dim obj As New Class1() Try obj.Test(-1) Catch e As ArgumentException Console.WriteLine("Exception: " & e.Message) End Try Console.WriteLine("All done") End Sub End Module

    The first line imports the MyClass namespace into the program. This line does the same job as using namespace does in C++, so you don’t have to fully qualify the name Class1 when it appears. The first line in the Main function creates a new Class1 object, and it’s equivalent to creating an object in C++ using new. The call to the Test function is enclosed in a try and catch construct, and you can see the similarity between the way exceptions are handled in Visual Basic and C++. The main difference is that in Visual Basic, the catch blocks are inside the try block.

  7. Build the code, and execute it. Passing -1 through as the argument will trigger the exception, and you should see the message printed out in the catch block.

Microsoft Visual C++  .NET(c) Step by Step
Microsoft Visual C++ .NET(c) Step by Step
ISBN: 735615675
Year: 2003
Pages: 208 © 2008-2017.
If you may any questions please contact us: