next up previous contents index Api Refernce
Next: 7 Copyright Up: 6 Usage Previous: 6.2.3 Reading a user


6.3 Error handling

In case of a command line usage error, a CmdLineException is thrown.

CmdLineException is derived from std::exception.

It has the following member functions:

A complete example with exception handling looks like this:


#include <iostream>
#include <string>

#include "cmdline.h"

using namespace std;
using namespace cmdl; // defined in cmdline.h

void PrintHelp(){/* like above */}
void AddStringToList(const string& s){/* like above */}

int main(int argc, char *argv[])
{
   try
   {                                                                                                           
      CmdLine CL;
      int anInt; 

      CL.Init( --argc, ++argv ); // skip program name

      CL.Call( "-h", PrintHelp );
      CL.GetSingleValue( "-N", anInt );
      CL.Call( "-S", AddStringToList,  CmdLine::cmdMultipleArgs );

      CL.Done();
   }

   // of course you may also catch an "exception"
   catch (CmdLineException& e) 
   {
      cerr << "error message:  " << e.what() << endl
           << "error code:     " << e.get_error_code() << endl
           << "cmdline option: " << e.get_option() << endl;
   }
}


next up previous contents index Api Refernce
Next: 7 Copyright Up: 6 Usage Previous: 6.2.3 Reading a user
Christian Neise
2003-01-12