E57 Foundation API v1.1.312  Aug. 10, 2011
Functions
examples/E57ExceptionFunctions.cpp File Reference

example: get info about E57Exceptions More...

Include dependency graph for E57ExceptionFunctions.cpp:

Functions

int main (int, char **)
 Example use of E57Exception functions.

Detailed Description

example: get info about E57Exceptions

Also see listing at end of this page for source without line numbers (to cut&paste from).

00001 /*** E57ExceptionFunctions.cpp example: get info about E57Exceptions */
00004 #include <iostream>
00005 #include "E57Foundation.h"
00006 using namespace e57;
00007 using namespace std;
00008 
00010 int main(int /*argc*/, char** /*argv*/) {
00011     const char* fileName = "doesnt_exist.e57";
00012     try {
00013         ImageFile imf(fileName, "r");
00014         imf.close(); // don't forget to explicitly close the ImageFile
00015     } catch(E57Exception& ex) {
00016         cout << "errorCode   = " << ex.errorCode() << endl;
00017         cout << "errorString = " << E57Utilities().errorCodeToString(ex.errorCode()).c_str() << endl;
00018         cout << "context     = " << ex.context()   << endl;
00019         cout << "what        = " << ex.what()      << endl;
00020 
00021         cout << "sourceFileName     = " << ex.sourceFileName()     << endl;
00022         cout << "sourceFunctionName = " << ex.sourceFunctionName() << endl;
00023         cout << "sourceLineNumber   = " << ex.sourceLineNumber()   << endl;
00024 
00025         cout << endl;
00026         ex.report(__FILE__, __LINE__, __FUNCTION__);
00027 
00028         cout << endl;
00029         if (ex.errorCode() == E57_ERROR_OPEN_FAILED)
00030             cout << "File " << fileName << " not found." << endl;
00031 
00032         return(-1);
00033     }
00034     return(0);
00035 }

This example program demonstrates several functions for extracting information out of an E57Exception object thrown by the API. See the HelloWorld.cpp example for discussion of the use of include files, constructing an ImageFile, and the try/catch block to handle exceptions.

Source line 13 attempts to read a file that doesn't exist, hence causing an E57Exception to be thrown with an errorCode of E57_ERROR_OPEN_FAILED. Because of the throw, source line 14 doesn't execute, and execution resumes on source line 16. Source lines 16-23 extract various fields from the E57Exception and print them on the console. In output line 3, the context string can have some very useful values of variables near where the exception was thrown. These variable values can be useful in debugging. The context string format might be useful for a programmer, but not necessarily useful for an end-user of the software. Output lines 5-7 show information about where the exception was thrown in the implementation internal source code.

Source line 26 uses a single E57Exception function, E57Exception::report, that prints out all the information to the console shown on output lines 9-15, as well as the source location where the exception catch is located in the user's source. The format of output lines 14-15 is chosen so that smart text editors (e.g. GNU emacs) can interpret the lines as error messages and create an automatic link, that when clicked on, opens the given file at the line number there the exception was thrown, or caught. Different editors may require a different formatting of the lines to create the clickable link.

Source lines 29-30 illustrate the printing of a custom error message based on the value of the error code. The errorCode could also have been tested in a switch statement.

The following console output is produced:

The XML section of the temp._e57 E57 file produced by this example program is as follows:

Here is the source code without line numbers to cut&paste from:

/*** E57ExceptionFunctions.cpp example: get info about E57Exceptions */
#include <iostream>
#include "E57Foundation.h"
using namespace e57;
using namespace std;

int main(int /*argc*/, char** /*argv*/) {
    const char* fileName = "doesnt_exist.e57";
    try {
        ImageFile imf(fileName, "r");
        imf.close(); // don't forget to explicitly close the ImageFile
    } catch(E57Exception& ex) {
        cout << "errorCode   = " << ex.errorCode() << endl;
        cout << "errorString = " << E57Utilities().errorCodeToString(ex.errorCode()).c_str() << endl;
        cout << "context     = " << ex.context()   << endl;
        cout << "what        = " << ex.what()      << endl;

        cout << "sourceFileName     = " << ex.sourceFileName()     << endl;
        cout << "sourceFunctionName = " << ex.sourceFunctionName() << endl;
        cout << "sourceLineNumber   = " << ex.sourceLineNumber()   << endl;

        cout << endl;
        ex.report(__FILE__, __LINE__, __FUNCTION__);

        cout << endl;
        if (ex.errorCode() == E57_ERROR_OPEN_FAILED)
            cout << "File " << fileName << " not found." << endl;

        return(-1);
    }
    return(0);
}
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines