4.2. The Error Class


Figure 4-1. class Error

namespace Oracle
{
    struct Error: public exception
    {
        // constructors
        Error(
	    const string&,
            const string&,
            const string& = "",
            const int = 0);
        Error(const Error&);

        // accessors
        string str() const;

        // data members
        string module;
        string msg;
        string fname;
        int line;
        ostringstream desc;
    };

    ostream& operator<<(ostream&, const Error&);
}

4.2.1. Public Member Functions

4.2.1.1. Constructors

Error(const string& module_name, const string& error_message, const string& file_name, const int line_number);

Error(const Error& initial_value);

When you throw an Error object, you specify the module_name and the error_message, and optionally the file_name and line_number. You may specify the file name and line number using the pre-defined macros __FILE__ and __LINE__.

If you wish to provide addtional information, such as the names and values of local variables, you may write to the desc data member as you would any output stream.

4.2.1.2. str()

string& str(void);

This function returns a formatted string containing the values of all data members that have been set. It may look like any of the following:

  • In module_name: error_message

  • In module_name (file file_name): error_message

  • In module_name (file file_name, line line_number): error_message description

The description is the value of desc and, if provided, follows the error message on a new line. Any of the three examples above may or may not display the description.

4.2.2. Public Data Members

4.2.2.1. module

This is the module or function name and is required by the constructor.

4.2.2.2. msg

This is the error message and is required by the constructor.

4.2.2.3. fname

This is the name of the source file where the error was thrown. A value of empty (""), the default, will be considered by the str() function to be unspecified.

4.2.2.4. line

This is the line number where the error was thrown. A value of zero, the default, will be considered by the str() function to be unspecified.

4.2.2.5. desc

This ostringstream can be filled with additional descriptive information, such as the names and values of local variables.

4.2.3. Non-Member Functions

4.2.3.1. operator<<()

ostream& operator<<(ostream& output_stream, const Error& error_object);

This function provides the ability to write the results of the str() directly to an output stream.