Resource Management in C++: Listing 4

Main program, this time using RAII.

// main.cpp (RAII version)
#include <iostream>
#include <memory>
#include <stdexcept>
#include "resource_access_object.hpp"

using namespace std;

void alpha()
{
  resource_access_object resource_handler;
  resource_handler.do_alpha();
}

void beta()
{
  resource_access_object resource_handler;
  resource_handler.do_beta();
}

int main()
{
  try
  {
    alpha();
    beta();
  }
  catch (exception& ex)
  {
    cout << "Exception: " << ex.what();
    // Other code to handle exception...
    // ...
  }

  return 0;
}

About the Author

Diego Dagum is a software architect and developer with more than 20 years of experience. He can be reached at email@diegodagum.com.