Original Post
I have put together an exception class of my own (code below if you're interested), that allows me to catch exceptions, feed error strings to a log file, and effectively identify where things go awry. The problem is, my code ends up either being in one huge try...catch block or numerous try...catch blocks around basically every fuction call. What am I missing? Is it fundamentally wrong to use exceptions in this way? -Kirk Please use [source lang="cpp"][/source] tags for large chunks of source code. Thanks - Emmanuel ExceptionRKD.h ExceptionRKD.cpp [Edited by - Emmanuel Deloget on March 15, 2007 10:14:45 AM]
/* ExceptionRKD
Robert Kirk DeLisle
Purpose: Class to be used in exception handling.
Modification History:
*/
#if !defined(ExceptionRKD_06May2004_INCLUDED)
#define ExceptionRKD_06May2004_INCLUDED
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <ostream>
#include <string>
using namespace std;
class ExceptionRKD
{
private:
long m_lErrCode; //contains an error code value
const char *m_cMessage; //an error message
const char *m_cFunction; //will contain the name of the function which generated the error
ExceptionRKD(); //force a construction method
public:
ExceptionRKD(long ErrCode, const char *FunctionName, const char *Message)
{ m_lErrCode=ErrCode; m_cMessage=Message; m_cFunction=FunctionName; }
long ErrorCode() const;
//return the error code currently in the exception
string ErrorMessage() const;
//return the error message currently in the exception
string ErrorFunction() const;
//return the name of the function currently in the exception
~ExceptionRKD() {}
friend ostream& operator<<(ostream& s, const ExceptionRKD &ex);
};
#endif
/* ExceptionRKD implementation
Robert Kirk DeLisle
Purpose: Class to be used in exception handling.
Modification History:
*/
#include "ExceptionRKD.h"
ostream& operator<<(ostream &s, const ExceptionRKD &ex)
{
/* Robert Kirk DeLisle
Purpose: Allow streaming output of exception contents.
Parameters: s - reference to the ostream object
ex - reference to the ExceptionRKD object
Return:
*/
s << ex.m_lErrCode << " : " << ex.m_cFunction << " : " << ex.m_cMessage;
return s;
}
long ExceptionRKD::ErrorCode() const
{
/* Robert Kirk DeLisle
Purpose: Return the ErrorCode value
Parameters: none
Return: The error code value currently in the exception.
*/
return m_lErrCode; //contains an error code value
}
string ExceptionRKD::ErrorMessage() const
{
/* Robert Kirk DeLisle
Purpose: return the error message currently in the exception
Parameters: none
Return: The error message currently in the exception.
*/
return string(m_cMessage);
}
string ExceptionRKD::ErrorFunction() const
{
/* Robert Kirk DeLisle
Purpose: return the name of the function currently in the exception
Parameters: none
Return: The name of the function currently in the exception.
*/
return string(m_cFunction);
}