Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What is a first chance exception? (And: How shall I handle it?)

0 views
Skip to first unread message

Spider

unread,
Oct 30, 1996, 3:00:00 AM10/30/96
to

I'm making my first attempts to wirte Windows programs using Visual C++
4.2 und MFC.

At the moment, I'm trying to access a database using ODBC. In the online-
help, there is a tutorial sample called "Enroll". I modified this sample
for my own program.

But sometimes, when I run my application in debugging mode, I got the
message "First chance exception in ...".

Sometimes, in the original Enroll tutorial example, the same message
appears.

My questions:

1) What exactly is a "first-chance exception"?
2) How can I find out, what code causes this exception?
3) Do I have to handle a "first-chance exception" and if yes, how?


Who can help me?

Jim Leavitt

unread,
Oct 31, 1996, 3:00:00 AM10/31/96
to

I> 1) What exactly is a "first-chance exception"?
I believe its a message from MFC

> 2) How can I find out, what code causes this exception?

Trace through the code in the debugger

> 3) Do I have to handle a "first-chance exception" and if yes, how?

You can safely ignore "first chance exception". I believe there's a
statement
on this from Mike Blaszczak of Microsoft in the mfc-l archives.

Jim Leavitt


dave porter

unread,
Oct 31, 1996, 3:00:00 AM10/31/96
to


Spider <ui...@rzstud1.uni-karlsruhe.de> wrote in article
<MPG.ce19ed4c12e31e5989681@news>...


> I'm making my first attempts to wirte Windows programs using Visual C++
> 4.2 und MFC.
>
> At the moment, I'm trying to access a database using ODBC. In the online-
> help, there is a tutorial sample called "Enroll". I modified this sample
> for my own program.
>
> But sometimes, when I run my application in debugging mode, I got the
> message "First chance exception in ...".
>
> Sometimes, in the original Enroll tutorial example, the same message
> appears.
>
> My questions:
>

> 1) What exactly is a "first-chance exception"?

An exception that is caught by the first-chance exception handler.
The first-chanceness is not an attribute of the exception, it
is an attribute of the handler. The debugger sets up a first-chance
handler ("first-chance" means it gets to see the exception
before anyone else).

> 2) How can I find out, what code causes this exception?

Use the debugger! Look at the call stack when the exception
is caught.

> 3) Do I have to handle a "first-chance exception" and if yes, how?

You might have to handle the exception, but if you do, it won't
be a first-chance exception. We can't tell without knowing what
the exception is. Maybe you're causing a memory access violation
because of a bug in your code. If so, then I wouldn't bother handling
the exception, I'd fix the code instead. Maybe some piece of code is
already handling that exception, and if you simply "go" from the
debugger, you'll let the handler handle it.

>
> Who can help me?
>

You can, by looking up "Exception Handling" in the documentation
which comes with Visual C++ (I think; I suppose it could have
come with MSDN).

Dan Vallejo <Microsoft>

unread,
Oct 31, 1996, 3:00:00 AM10/31/96
to

A first chance exception is an exception caught by the debug.
And don't ignore exceptions, use the debug and the stack to see where the
problem is being caused.
--

Dan Vallejo <Dan...@Microsoft.com>
Software Development Engineer, Money98!
The opinions expressed in this message are my own personal views and do not
reflect the official views of Microsoft Corporation.

Jim Leavitt <ji...@halcyon.com> wrote in article
<01bbc6f5$126ab1d0$e5203fce@jim_leavitt>...


> I> 1) What exactly is a "first-chance exception"?
> I believe its a message from MFC
>

> > 2) How can I find out, what code causes this exception?

> Trace through the code in the debugger
>

> > 3) Do I have to handle a "first-chance exception" and if yes, how?

David A. Céspedes M.

unread,
Oct 31, 1996, 3:00:00 AM10/31/96
to

First-change exception are usually a throw performed by Controls Boxes when they are validated and an invalid entry is found; i.e. an out of range value on an Input CEdit control.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
DaveedC
email: david.c...@laitram.com
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Spider

unread,
Nov 5, 1996, 3:00:00 AM11/5/96
to

In article <01bbc753$bd203c40$5b227392@glastonbury>, por...@ultranet.com
says...


> You might have to handle the exception, but if you do, it won't
> be a first-chance exception. We can't tell without knowing what
> the exception is. Maybe you're causing a memory access violation
> because of a bug in your code. If so, then I wouldn't bother handling
> the exception, I'd fix the code instead. Maybe some piece of code is
> already handling that exception, and if you simply "go" from the
> debugger, you'll let the handler handle it.

The following line

AFX_ODBC_CALL(::SQLExtendedFetch(m_hstmt, wFetchType,
nRow, pdwRowsFetched, m_rgRowStatus));

is causing the exceptions

First-chance exception in ODBCTest.exe (ODBCCR32.DLL): 0xE06D7363:
Microsoft C++ Exception.
First-chance exception in ODBCTest.exe (ODBCCR32.DLL): 0xE06D7363:
Microsoft C++ Exception.


The code is part of

RETCODE CRecordset::FetchData(UWORD wFetchType, SDWORD nRow,
DWORD* pdwRowsFetched)

in the MFC-file DBCORE.CPP.


The exception is thrown if I am at the beginning of my table and I am
choosing the "Previous record" menu item (althought there is no previous
record).

I wonder what I am doing wrong. Above all, the exception is also thrown
in the original tutorial sample named "enroll" from the ODBC-tutorial in
the online help.

Is this exception normal or what is going wrong?


Frank


Dan Vallejo <Microsoft>

unread,
Nov 5, 1996, 3:00:00 AM11/5/96
to

> The exception is thrown if I am at the beginning of my table and I am
> choosing the "Previous record" menu item (althought there is no previous
> record).
That is your answer. It throws if you go beyond the end of the table as
well.

--

Dan Vallejo <Dan...@Microsoft.com>
Software Development Engineer, Money98!
The opinions expressed in this message are my own personal views and do not
reflect the official views of Microsoft Corporation.

Spider <ui...@rzstud1.uni-karlsruhe.de> wrote in article
<MPG.ce8abd5138978f3989683@news>...

dave porter

unread,
Nov 6, 1996, 3:00:00 AM11/6/96
to


Jim Leavitt <ji...@halcyon.com> wrote in article
<01bbc6f5$126ab1d0$e5203fce@jim_leavitt>...
> I> 1) What exactly is a "first-chance exception"?
> I believe its a message from MFC
>

No, it is a message from the debugger's first-chance exception
handler saying that there has been an exception. The debugger
catches exceptions precisely to tell you about them. Exceptions
may or may not be bugs; only the original programmer can tell
you.

If you're not running with the debugger, you won't have a first-chance
exception handler, but you'll still have an exception. If nothing
bad seems to happen, then probably it isn't a bug and probably it
was quietly handled. But only the original programmer knows for sure.

The actual cause of the exception is described by the "exception
code", which the debugger will display. For example, a memory
access violation.

> > 2) How can I find out, what code causes this exception?
> Trace through the code in the debugger
>
> > 3) Do I have to handle a "first-chance exception" and if yes, how?
> You can safely ignore "first chance exception". I believe there's a
> statement
> on this from Mike Blaszczak of Microsoft in the mfc-l archives.
>

No, you can't safely ignore first-exceptions. Try running this code

char* p = NULL;
*p = 'X';

Now see how far you get by ignoring the exception. The Blaszczak
article details one particular circumstances in which one particular
piece of code is designed to raise an exception during normal
operation. That one you can ignore - because you have good
information to the effect that it is harmless. Others you ignore
at your peril.

There's no big mystery here. It's just like any other aspect of
programming. Can I ignore the fact that the characters "9876543210"
suddenly appear on my console window? Well, in general, there's
no good answer. It depends on whether my program contains code
which intends to write "9876543210" in normal operation.


Mike Thomas Jakobsen

unread,
Nov 12, 1996, 3:00:00 AM11/12/96
to

"dave porter" <por...@ultranet.com> wrote in a response to Spider:

>Spider <ui...@rzstud1.uni-karlsruhe.de> wrote in article

><MPG.ce19ed4c12e31e5989681@news>...
>> I'm making my first attempts to wirte Windows programs using Visual C++
>> 4.2 und MFC.
>>
>> At the moment, I'm trying to access a database using ODBC. In the online-
>> help, there is a tutorial sample called "Enroll". I modified this sample
>> for my own program.
>>
>> But sometimes, when I run my application in debugging mode, I got the
>> message "First chance exception in ...".
>>
>> Sometimes, in the original Enroll tutorial example, the same message
>> appears.
>>
>> My questions:
>>

>> 1) What exactly is a "first-chance exception"?

>An exception that is caught by the first-chance exception handler.

>The first-chanceness is not an attribute of the exception, it
>is an attribute of the handler. The debugger sets up a first-chance
>handler ("first-chance" means it gets to see the exception
>before anyone else).

>> 2) How can I find out, what code causes this exception?

>Use the debugger! Look at the call stack when the exception
>is caught.

>> 3) Do I have to handle a "first-chance exception" and if yes, how?

>You might have to handle the exception, but if you do, it won't


>be a first-chance exception. We can't tell without knowing what
>the exception is. Maybe you're causing a memory access violation
>because of a bug in your code. If so, then I wouldn't bother handling
>the exception, I'd fix the code instead. Maybe some piece of code is
>already handling that exception, and if you simply "go" from the
>debugger, you'll let the handler handle it.

>>
>> Who can help me?
>>

>You can, by looking up "Exception Handling" in the documentation
>which comes with Visual C++ (I think; I suppose it could have
>come with MSDN).

I'm actually getting the first chance exception in one of my programs
even though I handle the exception. Below is a listing of the
important parts of my program. It consists of a constructor,
CPhraseRelList(), that calls a member function, ReadRelationFile(),
within a try/catch block.
When an exception is generated from within ReadRelationFile() the
exception arrives perfectly in the constructor, but, in addition, some
other information arrives in the debug window. The
contents are listed at the bottom of the document.

Yours sincerely,
Mike Thomas Jakobsen
Sie...@inet.uni-c.dk
--------------------------------------------------------------------------------
Listing:
--------------------------------------------------------------------------------
#define _SZFAC sizeof(_TCHAR)
#define _NS sizeof('\0')

CPhraseRelList::CPhraseRelList(const _TCHAR* RelFname)
{
int len = _tcsclen(RelFname)*_SZFAC+_NS;
try {m_pRelFilename = new _TCHAR [len];}
catch (CMemoryException *e) {throw;}
_tcscpy(m_pRelFilename,RelFname);
m_pRelFilename[len-_NS] = '\0';
try {ReadRelationFile();}
catch (CMemoryException *e){
TRACE(_T("Memory exception occured\n"));
throw;
}
catch (CFileException* e){
TRACE(_T("File exception caught and passed on in CPhraseRelList
constructor\n"));
throw;
}

}

void CPhraseRelList::ReadRelationFile()
{
CStdioFile relfile;
// open the relation text file RelFname using a CStdioFile Object
CFileException cfe;
if (!relfile.Open(m_pRelFilename,CFile::modeRead|CFile::typeText,&cfe)
) {
TRACE(_T("File exception generated during CFile.Open() in
CPhraseList::ReadRelationFile()\n"));
AfxThrowFileException(cfe.m_cause,cfe.m_lOsError);
}

.......
}

----------------------------------------------------------------------------
Debug Window contents:
----------------------------------------------------------------------------
File exception generated during CFile.Open() in
CPhraseList::ReadRelationFile()
CFile exception: fileNotFound, File Unknown, OS error information = 2.
First-chance exception in PhraseTest.exe (MFC40D.DLL): 0xE06D7363:
Microsoft C++ Exception.
File exception caught in CPhraseRelList constructor and passed on

Raymond Chen

unread,
Nov 12, 1996, 3:00:00 AM11/12/96
to

On Tue, 12 Nov 1996 21:19:34 GMT, sie...@inet.uni-c.dk (Mike
Thomas Jakobsen) wrote:
> if (!relfile.Open(m_pRelFilename,CFile::modeRead|CFile::typeText,&cfe)
>) {
> TRACE(_T("File exception generated during CFile.Open() in
>CPhraseList::ReadRelationFile()\n"));
> AfxThrowFileException(cfe.m_cause,cfe.m_lOsError);
> }

This code is explicitly and intentionally throwing an exception.
A debugger first-chance exception occurs whenever an exception is
thrown, whether it be intentional or unintentional. It turns out
that in this case, the exception is intentional and is therefore
no cause for alarm.

The reason why the debugger stops is that you might need to debug
your exception handler, so you need a way to catch the exception
before your handler gets it.

--
(Note that my return address is intentionally invalid in order
to foil electronic mailing list generation software. Delete the
".---" to get my real address. No soliciting.)

Raymond Chen

unread,
Nov 12, 1996, 3:00:00 AM11/12/96
to
0 new messages