Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

C++ Exception-Handling Tricks for Linux

By Sachin O. Agrawal
2005-04-12


Retaining exception source information

In C++, whenever an exception is caught within a handler, the information about the source of the exception is lost. The exact source of the exception could provide a lot of vital information to better handle it, or the information could be appended to the error log for postmortem.

To deal with this, you can generate a stack trace in the constructor of the exception object during the throw exception statement. ExceptionTracer is a class that demonstrates this behavior.

Listing 1. Generating a stack trace in the exception object constructor

// Sample Program:

// Compiler: gcc 3.2.3 20030502
// Linux: Red Hat

#include
#include

#include
#include

using namespace std;

/////////////////////////////////////////////

class ExceptionTracer
{
public:
ExceptionTracer()
{
void * array[25];
int nSize = backtrace(array, 25);
char ** symbols = backtrace_symbols(array, nSize);

for (int i = 0; i < nSize; i++)
{
cout << symbols[i] << endl;
}

free(symbols);
}
};


Tutorial Pages:
» Four Techniques for Dealing with Built-in Language Limitations
» Retaining exception source information
» Managing Signals
» Managing Exceptions in Constructors and Destructors
» Handling Exceptions in Multi-Threaded Programs
» Conclusion
» Resources


First published by IBM DeveloperWorks


 | Bookmark
Related Tutorials:
» How to Install PHP 5 on Linux
» How to Install Apache 2 on Linux
» How to Install MySQL 5.0 on Linux
» SMB Caching
» Mound --Bind
» Tar Wild Card Interpretation

Ask A Question
characters left.