Home > Articles > Programming > C/C++

C++ Modularity with Namespaces and Exception Handling

  • Print
  • + Share This
Did you know that there are three different C++ namespace types? Namespaces, which are simple to learn and apply, offer a clever means of packaging your code and can assist when symbol conflicts occur. And how well does your code handle exceptions? Inserting exception-handling code can make your code more ready for packaging as external functions. Stephen Morris contends that just as namespaces offer an efficient way to package code, the skillful use of exception handling can quickly simplify that code.
Like this article? We recommend

Like this article? We recommend

The advent of XML has highlighted the importance of namespaces in the data domain. Namespaces also play an important role in C++, specifically in compartmentalizing code. They’re easy to use and they provide numerous benefits such as readability and improved modularity. C++ provides excellent support for handling exceptions.

Namespace code improves the structure of C++ programs, whereas exception handling improves the associated dynamic behavior. Both are useful additions to your C++ arsenal.

Companion Code

As usual in my articles, the code can be downloaded (see the end of this article for the link). The code consists of two files that illustrate the namespace text:

  • EventHandler.cpp
  • EventHandler.h

It also includes one file that illustrates C++ exception handling:

  • SomeExceptions.cpp

If you’re using Visual C++, feel free to create two Win32 Console applications, drop the first two files into the first workspace and build. Then do the same for the second file (SomeExceptions.cpp). The code supplied should compile and run.

  • + Share This
  • 🔖 Save To Your Account