[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

try-catch block and unhandled exception

Posted on 2000-01-05
5
Medium Priority
?
1,596 Views
Last Modified: 2012-08-13
I have an MFC app with the following code:

  CDatabase* cdbDatabase = new CDatabase;
  cdbDatabase->OpenEx("DSN=OracleTest;UID=scott;PWD=tiger;");
  cout << cdbDatabase->IsOpen() << endl;
  CRecordset* rs = new CRecordset(cdbDatabase);

  try
  {
    rs->Open(CRecordset::snapshot, "SELECT USER FROM DUAL;");
  }
  catch (CException e)
  {
    cout << "Caught exception" << endl;
  }
  catch (...)
  {
    cout << "Caught in ellipses" << endl;
  }

When run it outputs:
1
Caught in ellipses

If I take out the catch(...) statement I get a Microsoft Visual C++ error: Unhandled exception in db_test.exe (KERNEL32.DLL): 0xE06D7363: Microsoft C++ Exception.

What is causing this exception? It gets trapped in the catch(...) block but what do I do with it there? How do I access the error message?
0
Comment
Question by:jmnolan
  • 2
  • 2
5 Comments
 

Accepted Solution

by:
SDU earned 600 total points
ID: 2327391
You have to catch the exception like this

   catch (CDBException* e)
   {
            e->ReportError();
            e->Delete();
   }

If I remember well, you can't catch the exception
by value nor by reference, it doesn't work.

Regards
0
 
LVL 23

Expert Comment

by:chensu
ID: 2327398
The reason it flows to catch (...) is because the CRecordset::Open function throws either CDBException * or CMemoryException *. The correct way to handle it is

try
{
    rs->Open(CRecordset::snapshot, "SELECT USER FROM DUAL;");
}
catch (CDBException *pe)
{
    pe->Delete();
}
catch (CMemoryException *pe)
{
    pe->Delete();
}


or


try
{
    rs->Open(CRecordset::snapshot, "SELECT USER FROM DUAL;");
}
catch (CException *pe)
{
    pe->Delete();
}
0
 

Author Comment

by:jmnolan
ID: 2327444
Either answer would have worked for me. Thanks!!
0
 

Expert Comment

by:SDU
ID: 2328933
If, instead of displaying the error with ReportError, you want to get the error string, you can do it by getting this member from your exception

e->m_strError

0
 
LVL 23

Expert Comment

by:chensu
ID: 2329693
Calling CException::GetErrorMessage is better.
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

607 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