Mateusz Loskot :: hacking on, working out, living up

About handling FDO exceptions

08 Feb 2007 | mloskot

Every one who has played with Feature Data Objects (FDO) using C++ knows that FDO uses exceptions pretty intensively. Therefore, it’s important to understand how to handle exceptions thrown by FDO properly and effectively. A good introductory about exceptions handling can be found in The FDO Developer’s Guide document, under Development Practices and Exception Handling nodes. I’ll focus on practical examples here.

The first important thing to remember is that FDO uses its own hierarchy of exceptions with FdoException class as a base. The FdoException does not use or depend on standard C++ exceptions with std::exception base class and it has to be handled in FDO specific way - catch by pointer and released:

[cpp]try { // … } catch (FdoException* ex) { // Handling procedure (see below) ex->Release(); }[/cpp]

Second important thing and worth to remember is that FDO exceptions can nested. A source exception thrown in one place can be wrapped with new exception in catch clause on some level and re-thrown away. The new exception with can be caught on an upper level, wrapped and re-thrown again. Every such repetition of this wrap and re-throw operation nests source exception, together with associated message, in newly created. Here is a simple example presenting mechanism composing nested exceptions:

[cpp]try { try { try { throw FdoException::Create(L“First exception”); } catch (FdoException* ex) { throw FdoException::Create(L“Second caused by first”, ex); } } catch (FdoException* ex) { throw FdoException::Create(L“Third caused by second”, ex); } } catch (FdoException* ex) { // Handling procedure (see below) ex->Release(); }[/cpp]

Finally, it’s time to process queue of nested exceptions and output associated messages to a user. Below, catch clause completing the example above is presented. The output formatting is based on standard C++ streams:

[cpp]#include #include #include GetExceptionMessage() < < std::endl;

FdoPtr cause(ex->GetCause()); while(NULL != cause) { msg < < std::setw(++offset) << L“*” << cause->GetExceptionMessage() < < std::endl;

// get next exception from the queue
cause = cause->GetCause();

} ex->Release();

std::wcout < < msg << std::endl; }[/cpp]

The output message will look as follows:

  *** Third caused by second
   *** Second caused by first
    *** First exception

The first, root, exception is processed as last and the last re-thrown exception is processed as first. As it’s observable, FDO nested exceptions are recorder in LIFO queue order.

I hope this short explanation will be helpful to understand FDO exceptions. By the way, if you have any questions or proposals of articles about using Feature Data Objects with C++, please drop me a note about it. I’ll do my best to give some writings and examples here, so anyone can read about it.

Fork me on GitHub