ADODB:: my exceptions don't work

I have an object as follows:
class DatabaseReader  
{
public:
   DatabaseReader();
   virtual ~DatabaseReader();
...
...
private:
   bool  m_bFinished;
   ADODB::_ConnectionPtr m_pConn;
   ADODB::_RecordsetPtr  m_pRecS;
};

The constructor follows:
DatabaseReader::DatabaseReader()
:m_bFinished( false )
{
   try
   {
      HRESULT hr( m_pConn.CreateInstance( __uuidof(ADODB::Connection) ) );
          if( FAILED(hr) ) _com_issue_error( hr );
   }
   catch( _com_error & rComErr )
     {
          throw e_DatabaseException( "Can't create connection object, " + *rComErr.ErrorMessage() );
     }
     
   try
   {
      HRESULT hr( m_pRecS.CreateInstance( __uuidof(ADODB::Recordset) ) );
          if( FAILED(hr) ) _com_issue_error( hr );
   }
   catch( _com_error & rComErr )
     {
          throw e_DatabaseException( "Can't create record set object, " + *rComErr.ErrorMessage() );
     }
}

My problem is that, prior to, and during construction, I can raise exceptions to my heart's content. However, post construction, raising an exception causes an abnormal program termination. I am using MS VC++6.0 with service pack 3 applied. I cannot fully rule out a problem with the compiler, however perhaps someone can see what I have done wrong.
duncanlatimerAsked:
Who is Participating?
 
DanRollinsCommented:
I'll be glad to share the glory.  Post a 0-point message on community support and tell them that you want to divide the points 100-100 between DanRollins and icqad.

Remember to paste this link into the post.

http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20173513

Community support is here:

http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

-- Dan
0
 
AssafLavieCommented:
You're not showing up an example of when you encounter abnormal termination.
0
 
AlexReserCommented:
I think, it is a bad idea to make initialization of connection in constructor.
Instesd, make a function, that return true if connection is established.

DatabaseReader DBR; // constructs object
if (!DBR.MakeConnection(...))
{
   // error!!!
   // handle error and exit program
}
...  // normal work
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
peterchen092700Commented:
do you have a try/catch block around the code hat causes the abnormal program termination, and are you sure the type of exception that your catch waits for is thrown? (use a catch(...) to test this)

AlexReser: "Resource acquisition only in constructor" is one (tough) recommendation for "good OOP design"

Peter
0
 
DanRollinsCommented:

throw e_DatabaseException(
"Can't create record set object, "
 +
*rComErr.ErrorMessage()
);

This line adds two char pointers together, and using the (very ambiguous) result to construct an e_DatabaseException (whatever that is).

I'm surprized the compiler didn't box your ear for that, perhaps all warnings are turnd off.  Anyway, see if the error goes away if you change both occurrences to...

   throw e_DatabaseException( "test message" );

and place a breakpoint on them to see if they are getting hit for some reason.

-- Dan
0
 
peterchen092700Commented:
Dan the eagle eye ;)
0
 
jcgdCommented:
You are using pointers (m_pConn,m_pRecS) without initialize.
0
 
duncanlatimerAuthor Commented:
I have discovered the cause of my problem:
My destructor was closing the connection and recordset smart pointers, however they weren't necessarily open! (The exceptions were causing the destructor to be called)

I fixed it by initialising the pointers to NULL so that I could check whether they had been initialised and opened before closing them, like so:

DatabaseReader::~DatabaseReader()
{
if( m_pRecS != NULL && m_pRecS->State() == ADODB::adStateOpen ) m_pRecS->Close();
   if( m_pConn != NULL && m_pConn->State() == ADODB::adStateOpen ) m_pConn->Close();
}

So icqd technically you were the closest, and yes Dan those exceptions wouldn't have worked so I've fixed them too.
icqd and Dan, you can fight amounst yourselves for the points and the victor can post a post a comment which I'll accept as the answer.
0
 
peterchen092700Commented:
hey! I wanted to see a dogfight, not a wimpy cooperative solution ;)
0
 
modderCommented:
duncanlatimer,

OK. I've reduced the points on this question to 100

You can now award the question to one of the experts that you want to give points.

The points I refunded to you you can use to post dummy questions. Just title them
"Points for <expert ID>".

In the question body, just state:
"http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20173513 "

Once you've created the dummy question, you should find out what it's URL is. To do that, click on the "Other Questions" link at the top of this page, which brings you to the main page for this topic area. Your new dummy question will be listed at the top. Then, click on the dummy question to find out its URL, which will look a bit like this:
http://www.experts-exchange.com/jsp/qShow.jsp?ta=NAME_OF_TOPIC_AREA_HERE&qid=XXXXXXXXXXX (a number)

Finally, come back to this question and post a comment saying "Posted dummy question for <Expert ID>", you can find it here: <url of dummy question>

Thanks for your cooperation

modder
Community Support
0
 
duncanlatimerAuthor Commented:
it was a good spot
0
 
modderCommented:
Don't forget to post the URL for the dummy question here so that your other expert can find it easily.
0
 
jcgdCommented:
where are my points???...
0
 
modderCommented:
duncanlatimer,

I trust this is an oversight and nothing more. I won't be impressed as you used your request to split points in order to get 100 of them refunded.
0
 
modderCommented:
sorry - "if you used", not "as you used"
0
 
DanRollinsCommented:
I have to admit....I was in on the scam.  I knew I'd get caught.

Bummer.

-- Dan
0
 
duncanlatimerAuthor Commented:
For modder:
see http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=cplusprog&qid=20175737

Is this really happening? I thought I was doing the right thing by offering the points to two people who had been helpful to me, I chose to accept their comments as answers, both have collected them and I am now getting grief from the moderator, do you want some points as well?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.