Solved

ADODB::   my exceptions don't work

Posted on 2001-08-24
17
730 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
  • 4
  • 3
  • 3
  • +4
17 Comments
 
LVL 4

Expert Comment

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

Expert Comment

by:AlexReser
Comment Utility
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
Comment Utility
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
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility

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
Comment Utility
Dan the eagle eye ;)
0
 
LVL 3

Expert Comment

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

Author Comment

by:duncanlatimer
Comment Utility
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 100 total points
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 7

Expert Comment

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

Expert Comment

by:modder
Comment Utility
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
Comment Utility
it was a good spot
0
 
LVL 3

Expert Comment

by:modder
Comment Utility
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
Comment Utility
where are my points???...
0
 
LVL 3

Expert Comment

by:modder
Comment Utility
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
Comment Utility
sorry - "if you used", not "as you used"
0
 
LVL 49

Expert Comment

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

Bummer.

-- Dan
0
 

Author Comment

by:duncanlatimer
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 how to clear a vector as well as how to detect empty vectors in C++.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now