Exception thrown opening connection 
Author Message
 Exception thrown opening connection

Hi,

I have a problem when I try to catch a _com_error while opening a
_Connection. Mi code looks like:

int  function () {
    _ConnectionPtr spCON;
    try {
        CREATEiNSTANCE(spCON,Connection);
        spCON->Open( myConnectionString, "", "", -1 );
        spCON->Close();
    } catch( _com_error &e) {
        _bstr_t bstrSource(e.Source());
        _bstr_t bs = _bstr_t(" Error: ") + _bstr_t(e.Error()) + _bstr_t("
Msg: ") + _bstr_t(e.ErrorMessage()) + _bstr_t(" Description: ") +
_bstr_t(e.Description());
        MessageBox(0,bs,bstrSource, MB_OK);
    }
    return -1;

Quote:
}

Well, when the "return" executes, I get an "Unhandled exception" message if
I run the code from within the VC environment, and if I run the exe from the
command line, the App finishes. I dont understand this behaviour, because
it is possible that you must do some work even if you cannot open the
database. Im sure that I'm doing something wrong. Could you help me?

--
Juan Francisco Navarro Peso
JUSAN, S.A.
Tlf: +34 91 456 0110 Ext.: 52
Fax: +34 91 553 1411



Fri, 27 Aug 2004 18:53:46 GMT  
 Exception thrown opening connection
Juan,

I think I would need just a bit more information.  For instance:

1) Is that all of the code?  I input the code that you have, ran it, and I did
not get any unhandled exceptions in the return.

2) Where is your call to ::CoInitialize?

3) What happens in the CREATEiNSTANCE macro?

4) What is the value of the member variable m_pInterface in spCON prior to the
call to the Open method?

best regards
Roy Fine.



Quote:
> Hi,

> I have a problem when I try to catch a _com_error while opening a
> _Connection. Mi code looks like:

> int  function () {
>     _ConnectionPtr spCON;
>     try {
>         CREATEiNSTANCE(spCON,Connection);
>         spCON->Open( myConnectionString, "", "", -1 );
>         spCON->Close();
>     } catch( _com_error &e) {
>         _bstr_t bstrSource(e.Source());
>         _bstr_t bs = _bstr_t(" Error: ") + _bstr_t(e.Error()) + _bstr_t("
> Msg: ") + _bstr_t(e.ErrorMessage()) + _bstr_t(" Description: ") +
> _bstr_t(e.Description());
>         MessageBox(0,bs,bstrSource, MB_OK);
>     }
>     return -1;
> }

> Well, when the "return" executes, I get an "Unhandled exception" message if
> I run the code from within the VC environment, and if I run the exe from the
> command line, the App finishes. I dont understand this behaviour, because
> it is possible that you must do some work even if you cannot open the
> database. Im sure that I'm doing something wrong. Could you help me?

> --
> Juan Francisco Navarro Peso
> JUSAN, S.A.
> Tlf: +34 91 456 0110 Ext.: 52
> Fax: +34 91 553 1411




Sat, 28 Aug 2004 11:30:56 GMT  
 Exception thrown opening connection
Hi, Roy:

You can assume that all works fine (I call CoInitialize(), I have a valid
interface pointer, etc.). The problem is very simple: it happens when I have
an erroneous connection string (for example, an invalid path for an Access
mdb file). Then, I can catch one exception, but when my code reaches the
"return" line, I get another "Unhandled exception" message.

Thank you.

--
Juan Francisco Navarro Peso
JUSAN, S.A.
Tlf: +34 91 456 0110 Ext.: 52
Fax: +34 91 553 1411



Quote:
> Juan,

> I think I would need just a bit more information.  For instance:

> 1) Is that all of the code?  I input the code that you have, ran it, and I
did
> not get any unhandled exceptions in the return.

> 2) Where is your call to ::CoInitialize?

> 3) What happens in the CREATEiNSTANCE macro?

> 4) What is the value of the member variable m_pInterface in spCON prior to
the
> call to the Open method?

> best regards
> Roy Fine.



> > Hi,

> > I have a problem when I try to catch a _com_error while opening a
> > _Connection. Mi code looks like:

> > int  function () {
> >     _ConnectionPtr spCON;
> >     try {
> >         CREATEiNSTANCE(spCON,Connection);
> >         spCON->Open( myConnectionString, "", "", -1 );
> >         spCON->Close();
> >     } catch( _com_error &e) {
> >         _bstr_t bstrSource(e.Source());
> >         _bstr_t bs = _bstr_t(" Error: ") + _bstr_t(e.Error()) +
_bstr_t("
> > Msg: ") + _bstr_t(e.ErrorMessage()) + _bstr_t(" Description: ") +
> > _bstr_t(e.Description());
> >         MessageBox(0,bs,bstrSource, MB_OK);
> >     }
> >     return -1;
> > }

> > Well, when the "return" executes, I get an "Unhandled exception" message
if
> > I run the code from within the VC environment, and if I run the exe from
the
> > command line, the App finishes. I dont understand this behaviour,
because
> > it is possible that you must do some work even if you cannot open the
> > database. Im sure that I'm doing something wrong. Could you help me?

> > --
> > Juan Francisco Navarro Peso
> > JUSAN, S.A.
> > Tlf: +34 91 456 0110 Ext.: 52
> > Fax: +34 91 553 1411




Sat, 28 Aug 2004 16:30:00 GMT  
 Exception thrown opening connection
Juan,

Please see sample code at the end of this post.

I am not sure what the problem is.  I have included the test application
that I used (note that it is an console app), and as you can see, it is very
simple.  It does throw an exception when either the Data Source or the
Provider references invalid locations.  But the catch block is handling the
exception just fine.

If your application thows an exception after the return statement, then,
most likely, it is not something that was done wrong in the catch handler.
Remember that the _ConnectionPtr object is a smart pointer, and when it goes
out of scope, the c++ runtime will call the object's destructor (and will
call the d-tor for all C++ class and struct objects).

I suspect your problem is somewhere in code that you have not shown in your
post.

Best regards
Roy Fine

/* ************************* */
/* ************************* */
#include "stdafx.h"
wchar_t *cnxStr = L"Provider=Microsoft.Jet.OLEDB.4.0;Data
xSource=C:\\FT2000\\Dialer.mdb;";

int subFunction(){
_ConnectionPtr cnxion;
try{
 cnxion.CreateInstance(__uuidof(Connection));
 cnxion->Open(cnxStr,L"",L"",adConnectUnspecified);
 cnxion->Close();
 return 1;
 }
catch(_com_error &e){
 _bstr_t a = L"Description: " + _bstr_t(e.Description()) + L"
ErrorMessage:" + _bstr_t(e.ErrorMessage()) + L"  Error:" +
_bstr_t(e.Error()) + L"  Source:" +_bstr_t(e.Source());
 BSTR bstrMsg;
 e.ErrorInfo()->GetDescription(&bstrMsg);
 MessageBoxW(0,bstrMsg,L"Error",MB_OK);
 ::SysFreeString(bstrMsg);
 }
return 0;

Quote:
}

/* ************************* */
/* ************************* */
int main(int argc, char* argv[]){

::CoInitialize(NULL);
subFunction();
::CoUninitialize();
return 0;

Quote:
}



Sun, 29 Aug 2004 00:39:13 GMT  
 Exception thrown opening connection
Hi, Roy:

Have you tried your code with an inexistent mdb file? Try, for example, to
change the file name. Does it works properly? I've tested, and it throws an
Unhandled exception when the program reaches the line "return 0".

BR,

--
Juan Francisco Navarro Peso
JUSAN, S.A.
Tlf: +34 91 456 0110 Ext.: 52
Fax: +34 91 553 1411



Quote:
> Juan,

> Please see sample code at the end of this post.

> I am not sure what the problem is.  I have included the test application
> that I used (note that it is an console app), and as you can see, it is
very
> simple.  It does throw an exception when either the Data Source or the
> Provider references invalid locations.  But the catch block is handling
the
> exception just fine.

> If your application thows an exception after the return statement, then,
> most likely, it is not something that was done wrong in the catch handler.
> Remember that the _ConnectionPtr object is a smart pointer, and when it
goes
> out of scope, the c++ runtime will call the object's destructor (and will
> call the d-tor for all C++ class and struct objects).

> I suspect your problem is somewhere in code that you have not shown in
your
> post.

> Best regards
> Roy Fine

> /* ************************* */
> /* ************************* */
> #include "stdafx.h"
> wchar_t *cnxStr = L"Provider=Microsoft.Jet.OLEDB.4.0;Data
> xSource=C:\\FT2000\\Dialer.mdb;";

> int subFunction(){
> _ConnectionPtr cnxion;
> try{
>  cnxion.CreateInstance(__uuidof(Connection));
>  cnxion->Open(cnxStr,L"",L"",adConnectUnspecified);
>  cnxion->Close();
>  return 1;
>  }
> catch(_com_error &e){
>  _bstr_t a = L"Description: " + _bstr_t(e.Description()) + L"
> ErrorMessage:" + _bstr_t(e.ErrorMessage()) + L"  Error:" +
> _bstr_t(e.Error()) + L"  Source:" +_bstr_t(e.Source());
>  BSTR bstrMsg;
>  e.ErrorInfo()->GetDescription(&bstrMsg);
>  MessageBoxW(0,bstrMsg,L"Error",MB_OK);
>  ::SysFreeString(bstrMsg);
>  }
> return 0;
> }

> /* ************************* */
> /* ************************* */
> int main(int argc, char* argv[]){

> ::CoInitialize(NULL);
> subFunction();
> ::CoUninitialize();
> return 0;
> }



Tue, 31 Aug 2004 16:17:02 GMT  
 Exception thrown opening connection
Juan,

I just tried it with a Data Source value that does not exist -- the program
execution goes to the catch handler, processes normally, then through the
return statement of subFunction back to main, etc.  All with no unhandled
exceptions.

I am running Visual C++, version 6 with Service Pack 5, and with MDAC 2.6,
Service Pack 1 installed.  The operating system is Windows 2000 Professional
with Service Pack 2 installed.  The application is compiled with non-Unicode
setting.  The environment is a console app.

best regards,
Roy Fine



Quote:
> Hi, Roy:

> Have you tried your code with an inexistent mdb file? Try, for example, to
> change the file name. Does it works properly? I've tested, and it throws
an
> Unhandled exception when the program reaches the line "return 0".

> BR,

> --
> Juan Francisco Navarro Peso
> JUSAN, S.A.
> Tlf: +34 91 456 0110 Ext.: 52
> Fax: +34 91 553 1411



> > Juan,

> > Please see sample code at the end of this post.

> > I am not sure what the problem is.  I have included the test application
> > that I used (note that it is an console app), and as you can see, it is
> very
> > simple.  It does throw an exception when either the Data Source or the
> > Provider references invalid locations.  But the catch block is handling
> the
> > exception just fine.

> > If your application thows an exception after the return statement, then,
> > most likely, it is not something that was done wrong in the catch
handler.
> > Remember that the _ConnectionPtr object is a smart pointer, and when it
> goes
> > out of scope, the c++ runtime will call the object's destructor (and
will
> > call the d-tor for all C++ class and struct objects).

> > I suspect your problem is somewhere in code that you have not shown in
> your
> > post.

> > Best regards
> > Roy Fine

> > /* ************************* */
> > /* ************************* */
> > #include "stdafx.h"
> > wchar_t *cnxStr = L"Provider=Microsoft.Jet.OLEDB.4.0;Data
> > xSource=C:\\FT2000\\Dialer.mdb;";

> > int subFunction(){
> > _ConnectionPtr cnxion;
> > try{
> >  cnxion.CreateInstance(__uuidof(Connection));
> >  cnxion->Open(cnxStr,L"",L"",adConnectUnspecified);
> >  cnxion->Close();
> >  return 1;
> >  }
> > catch(_com_error &e){
> >  _bstr_t a = L"Description: " + _bstr_t(e.Description()) + L"
> > ErrorMessage:" + _bstr_t(e.ErrorMessage()) + L"  Error:" +
> > _bstr_t(e.Error()) + L"  Source:" +_bstr_t(e.Source());
> >  BSTR bstrMsg;
> >  e.ErrorInfo()->GetDescription(&bstrMsg);
> >  MessageBoxW(0,bstrMsg,L"Error",MB_OK);
> >  ::SysFreeString(bstrMsg);
> >  }
> > return 0;
> > }

> > /* ************************* */
> > /* ************************* */
> > int main(int argc, char* argv[]){

> > ::CoInitialize(NULL);
> > subFunction();
> > ::CoUninitialize();
> > return 0;
> > }



Wed, 01 Sep 2004 00:08:34 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Exception thrown when opening ADO recordset

2. Exception handling - how to know which exceptions are thrown

3. property throws Exception in visual designer!

4. Throwing Exceptions in Constructors

5. System.Drawing.SafeNativeMethods threw an exception

6. std::list.clear() throws exceptions in .NET 2003.

7. Function that only throws an exception

8. too many controls throw exception

9. OK to throw exception from constructor?

10. __raise throws exception

11. Behavior of throw() exception specification?

12. throw overflow exception

 

 
Powered by phpBB® Forum Software