?
Solved

ADODB::   my exceptions don't work

Posted on 2001-08-24
17
Medium Priority
?
749 Views
Last Modified: 2007-12-19
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.
0
Comment
Question by:duncanlatimer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +4
17 Comments
 
LVL 4

Expert Comment

by:AssafLavie
ID: 6423223
You're not showing up an example of when you encounter abnormal termination.
0
 
LVL 1

Expert Comment

by:AlexReser
ID: 6424512
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
 
LVL 7

Expert Comment

by:peterchen092700
ID: 6424662
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 49

Expert Comment

by:DanRollins
ID: 6425696

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
 
LVL 7

Expert Comment

by:peterchen092700
ID: 6426082
Dan the eagle eye ;)
0
 
LVL 3

Expert Comment

by:jcgd
ID: 6431082
You are using pointers (m_pConn,m_pRecS) without initialize.
0
 

Author Comment

by:duncanlatimer
ID: 6431374
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
 
LVL 49

Accepted Solution

by:
DanRollins earned 400 total points
ID: 6431456
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
 
LVL 7

Expert Comment

by:peterchen092700
ID: 6431736
hey! I wanted to see a dogfight, not a wimpy cooperative solution ;)
0
 
LVL 3

Expert Comment

by:modder
ID: 6432222
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
 

Author Comment

by:duncanlatimer
ID: 6437019
it was a good spot
0
 
LVL 3

Expert Comment

by:modder
ID: 6437072
Don't forget to post the URL for the dummy question here so that your other expert can find it easily.
0
 
LVL 3

Expert Comment

by:jcgd
ID: 6438629
where are my points???...
0
 
LVL 3

Expert Comment

by:modder
ID: 6439941
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
 
LVL 3

Expert Comment

by:modder
ID: 6439949
sorry - "if you used", not "as you used"
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6439998
I have to admit....I was in on the scam.  I knew I'd get caught.

Bummer.

-- Dan
0
 

Author Comment

by:duncanlatimer
ID: 6441018
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses
Course of the Month11 days, 8 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question