Solved

Can't catch C++ exception

Posted on 1998-03-27
6
564 Views
Last Modified: 2013-11-20
I have a case where I cannot catch exceptions generated by CRecordset::MoveNext().  Other exceptions are caught by my exception handler (catch block), but this one results in only the following message appearing in the Output window of the debugger.

First-chance exception in ... Microsoft C++ Exception.

Then control returns to the statement following the MoveNext() that caused the exception.  What might be going on?  Do you have any suggestions for debugging this problem?  Are we looking at possible stack corruption?

Thank you,
Bruce O'Reilly and Andre Nguyen
0
Comment
Question by:boreilly
6 Comments
 
LVL 8

Expert Comment

by:gelbert
ID: 1317286
When you are in debug mode in VC++ 5.0 go to menu item "debug" subiten "Exeptions". Select all listed type of exceptions and change status to "Stop Always". It make debugger stop when exceptions arise instead of displaying message in output window
0
 

Author Comment

by:boreilly
ID: 1317287
I have already tried what Gelbert suggested.  Using that, I see the exception being raised deep down in the ODBC DLL after CRecordSet::FetchData() calls ::SQLExtendedFetch().  The problem is, what could cause the exception to be uncatchable by my handler?
0
 
LVL 11

Expert Comment

by:alexo
ID: 1317288
The exception raised is not really a C++ exception.  Check the help about SEH (structured exception handling) and how to catch such exceptions in C++.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 5

Expert Comment

by:yonat
ID: 1317289
Maybe the exception is caught BEFORE it reaches your catch clause.

BTW, catch(...) catches all exceotions, including the structured exceptions that alexo mentioned.
0
 
LVL 11

Accepted Solution

by:
mikeblas earned 200 total points
ID: 1317290
The behaviour you note is entirely by-design and completely normal.

You're calling into some other functions. Those functions probably call other functions, too. When the exception is raised, the environment tries to find an exception handler. It searches by looking at the current function, the function that called it, the function that called that one, and so on--unwinding the stack of function calls.

You're not hitting your own catch() block because some other catch() block is handling the exception, and that's that. The message from the debugger is entirely normal: when an exception is thrown in a process that is being debugged, the debugger is notified _immediately_. The debugger can stop execution before the environment tries to find a handler, or it can let the thread that's throwing the exception try to find a handler.

Since the author of the ODBC driver you're using knew that an exception might happen, they wrote a catch() handler of their own to manage the execption. The exception is thrown and you see the message in the debugger. But the environment ends up finding the catch() handler the driver itself implements and executes that handler. The handler doesn't rethrow the exception, so processing continues on as normal. And that's that: you're not offered a chance to catch() the exception because someone else closer to the exception has already handled it. Even if you got the chance to handle the exception, you wouldn't know what to do: you don't have the source code for the driver, and the throw structure that the driver uses internally aren't documented.

If you've set the "Exceptions" option that GELBERT talks about to "Stop Always", the debugger will immediately break when the exception is thrown. That can help you debug the code generating the exception or the code which is going to find the exception handler.  Otherwise, if that option isn't set for the type of exception being generated, the debugger returns control immediately to the thread that threw the exception so it can search for a handler.

That's why a "first-chance exception" has it's name: the debugger is offered the first chance at handling a rasied exception. The message is just informational. If there is no debugger attached to the process, the thread will immediately begin search for a handler. If a handler is found, it executes. If no handlers is found, the operating system kicks the process out of memory with an "unhandled execption" error.

You can find lots of information about the mechanism at work here by reading.  See the KB article Q105675, for example, for a description of the messages the debugger generates.  Or, read up on the "Exception Handling" topics in the SDK--like "Exception Handling for Debugging".  Or, read the "Understanding the Debugger" article I wrote for Visual C++ Developer; see http://www.pinpub.com/vcd/home.htm for more information about that magazine.

The exception raised is _certainly_ a C++ exception; if it wasn't, it wouldn't have the "Microsoft C++ exception" type ID. An systems exception has a type ID describing the exact error that ocurred.

A catch(...) block will catch all thrown exception types that are scoped for it--it still won't catch exceptions of _any_ type that are handled by more deeply scoped catch() handlers.

.B ekiM

0
 

Author Comment

by:boreilly
ID: 1317291
Mike,
Thank you very much for the help.  What threw me off is that I could not find the exception handler when tracing this with the debugger, but that's not too surprising given that I don't have source to the Access '97 ODBC driver.  Your book is really great.  Every MFC programmer in this place owns an copy and we all use it regularly.  We just call it "Mike's book".

Bruce O'Reilly
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Replacement selected text 2 47
matchUp  challenge 6 48
abstract class with all non abstract mentods 6 71
FizzBuzz challenge 9 72
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

708 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

18 Experts available now in Live!

Get 1:1 Help Now