Re: confused by exception handling in VC 2008
"max" <maxxx126@gmail.com> wrote in message
news:34723640-020e-473c-8aed-1c2a7861e197@k36g2000pri.googlegroups.com...
I am using VC 2008 and I was trying the __try exception handling and I
bumped into the following 3 cases:
1)
__try {
char *p = 0;
*p = '\0';
AfxMessageBox("no exception caught");
}
__except (EXCEPTION_EXECUTE_HANDLER){
AfxMessageBox("exception caught");
}
the message "exception caught" is displayed as expected.
2)
__try {
int v[5];
v[10] = 1; // This vector access is out of range
AfxMessageBox("no exception");
}
__except (EXCEPTION_EXECUTE_HANDLER){
AfxMessageBox("exception caught");
}
no exception raised, the message "no exception" is displayed !
3)
__try {
char *p = 0;
int v[5];
v[10] = 1; // This vector access is out of range
*p = '\0';
AfxMessageBox("no exception");
}
__except (EXCEPTION_EXECUTE_HANDLER){
AfxMessageBox("exception caught");
}
In this case the application crashes, the exception handler failed.
Any ideas for the behaviour of cases 2) and 3) ?
Sure. In case 2 you got lucky and didn't trash anything important on the
stack (such as say, your return address). So no hardware exception was
raised (if you have stack-checking enabled you *may have* recieved a warning
about a corrupted stack - but that's not guranteed).
In case 3 you *did* trash something important on the stack and when the
initial exception was raised (*0=0), a second exception occured while the OS
was attempting to determine the context of the exception (ie. before your
__except block was entered). Probably you overwrote your return address or
something. Remember the stack grows DOWN so when you write beyond the range
of a local variable such as 'v' here, you're overwriting your existing stack
frame.
When an exception occurs within an exception the program aborts immediately
with an "abonormal program termination" message or, or Dr. Watson type
hardware crash dialog (or worse).
- Alan Carre