Click to See Complete Forum and Search --> : What's a "First-chance exception"?


Nick Gilbert
April 13th, 1999, 06:16 PM
Hi,

I'm using the CMemoryState class to help detect memory leaks, and when running my app in debug mode using F5, the debug window occasionally says something like:

"First-chance exception in SynClient.exe (KERNEL32.DLL): 0xC0000005: Access Violation."

Is this actally an error? I don't actually get a normal Access Violation, so what does it mean? Is it something I should worry about and try and fix?
And if so, how I go about working out what causes it?

Thanks!

Nick......

Code_Monkey
March 13th, 2005, 05:06 PM
Usually, when I get that error, code is closing a Windows handle that has already been closed; in that case, the error is not fatal, and I perform the following steps to avoid confrontation:

- Open the "Exceptions" menu item from the "Debug" menu and change the action for an "Invalid Handle" to "Stop if not handled."

darwen
March 13th, 2005, 05:47 PM
If the exceptions are being handled then you shouldn't normally have a problem. However you should try your best to track down what's causing the exception (usually because you're doing something wrong I might add) and fix it.

There are loads of first chance exceptions which get thrown and handled in the WMV replay code - from what I've seen - but I've not seen any adverse effects from them.

Just be aware that it's very likely that you're doing something wrong in your code and you should try to find out what. Especially with access violation exceptions which can have potentially deadly consequences.

Darwen.

John E
March 14th, 2005, 01:38 AM
If the OS encounters an exception when running your app (well, certain types of exception anyway) it gives your application the "first chance" at handling it. If your app doesn't handle the exception, it gets passed back to the OS to see if something else will handle it.

In my experience, first chance exceptions are rarely caused by your program. They can be caused by your program calling a DLL function which encounters an exception. Your program gets 'first chance' but it wasn't expecting the exception so it doesn't handle it. The exception then gets passed back to the DLL which possibly does handle it. Otherwise, the OS might be offered a chance to handle it. If nothing handles the exception at all, you'll get the dreaded "Unhandled Exception" message box.

Any exceptions caused by your program will generally result in the dreaded message box - because it's quite unlikely that they'll be handled by anything else.