|
||||||||
|
|
#1
|
|
I created the following code segment which recreates an interesting problem
with C++ exceptions. I am using Visual Studio C++ 6.0 Enterprise Edition. When the program runs in DEBUG mode the exception is caught in the F2 function handler as I would expect. However when the program runs in RELEASE mode the exeption is caught in the F1 function handler. The command that is throwing the exception is the strcspn(). Why does this occur? //----------------------------------------------------------------- #include <iostream.h> #include <string.h> class EClass { public: void F1(const char* a); void F2(const char* b); }; void EClass::F1(const char* a) { try { F2(a); } catch(...) { cout << "caught in F1 handler" << endl; } } void EClass::F2(const char* a) { int i; char* b = NULL; try { b = strstr(a, "Test"); i = strcspn(b, "\r\n"); } catch(...) { cout << "caught in F2 handler" << endl; } } int main(int argc, char* argv[]) { EClass ec; char a[200]; strcpy(a,"Tes\r\n"); try { ec.F1(a); } catch(...) { cout << "caught in main handler" << endl; } return 0; } //----------------------------------------------------------------- Tom --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.542 / Virus Database: 336 - Release Date: 11/18/2003 Tom Bruce |
|
#2
|
|||
|
|||
|
I think that your release mode have optimization ( check that and set no
optimization ) , otherwise it have to be same. BTW I see that all groups in your list are networking group , but better ask such questions on microsoft.public.vc.language or similar. Arkady "Tom Bruce" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)... > I created the following code segment which recreates an interesting problem > with C++ exceptions. I am using Visual Studio C++ 6.0 Enterprise Edition. > When the program runs in DEBUG mode the exception is caught in the F2 > function handler as I would expect. However when the program runs in > RELEASE mode the exeption is caught in the F1 function handler. The command > that is throwing the exception is the strcspn(). Why does this occur? > > //----------------------------------------------------------------- > #include <iostream.h> > #include <string.h> > > class EClass > { > public: > void F1(const char* a); > void F2(const char* b); > }; > > void EClass::F1(const char* a) > { > try > { > F2(a); > } > catch(...) > { > cout << "caught in F1 handler" << endl; > } > } > > void EClass::F2(const char* a) > { > int i; > char* b = NULL; > > try > { > b = strstr(a, "Test"); > i = strcspn(b, "\r\n"); > } > catch(...) > { > cout << "caught in F2 handler" << endl; > } > } > > int main(int argc, char* argv[]) > { > EClass ec; > char a[200]; > strcpy(a,"Tes\r\n"); > > try > { > ec.F1(a); > } > catch(...) > { > cout << "caught in main handler" << endl; > } > return 0; > } > //----------------------------------------------------------------- > > Tom > > > --- > Outgoing mail is certified Virus Free. > Checked by AVG anti-virus system (http://www.grisoft.com). > Version: 6.0.542 / Virus Database: 336 - Release Date: 11/18/2003 > > |
|
#3
|
|||
|
|||
|
I would normally send this reply only to the appropriate groups posted to in
your original, but since there are none of those... The try/catch constructs are designed to catch C++ exceptions, not structured exceptions, which is what strcspn() is throwing (note that in VC, C++ exceptions are built on top of the Windows structured exception handling facility). The fact that catch(...) in F1() catches the structured exception at all is really a bug which I believe is fixed in VC7. Why an exception is not being caught by catch(...) in F2() is due to compiler optimizations - the compiler knows that nothing in F2() is capable of throwing C++ exceptions, so it doesn't bother to compile in the try/catch. Check out "structured exception handling" in MSDN. If you replace the try/catch in F2() with __try/__except, you'll see that a structured exception is caught in F2(). {L} "Tom Bruce" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)... > I created the following code segment which recreates an interesting problem > with C++ exceptions. I am using Visual Studio C++ 6.0 Enterprise Edition. > When the program runs in DEBUG mode the exception is caught in the F2 > function handler as I would expect. However when the program runs in > RELEASE mode the exeption is caught in the F1 function handler. The command > that is throwing the exception is the strcspn(). Why does this occur? > > //----------------------------------------------------------------- > #include <iostream.h> > #include <string.h> > > class EClass > { > public: > void F1(const char* a); > void F2(const char* b); > }; > > void EClass::F1(const char* a) > { > try > { > F2(a); > } > catch(...) > { > cout << "caught in F1 handler" << endl; > } > } > > void EClass::F2(const char* a) > { > int i; > char* b = NULL; > > try > { > b = strstr(a, "Test"); > i = strcspn(b, "\r\n"); > } > catch(...) > { > cout << "caught in F2 handler" << endl; > } > } > > int main(int argc, char* argv[]) > { > EClass ec; > char a[200]; > strcpy(a,"Tes\r\n"); > > try > { > ec.F1(a); > } > catch(...) > { > cout << "caught in main handler" << endl; > } > return 0; > } > //----------------------------------------------------------------- > > Tom > > > --- > Outgoing mail is certified Virus Free. > Checked by AVG anti-virus system (http://www.grisoft.com). > Version: 6.0.542 / Virus Database: 336 - Release Date: 11/18/2003 > > |
|
#4
|
|||
|
|||
|
Because you didn't specify Asynchronous exceptions using /EHa if you don't
the compiler assumes that exceptions are only thrown on function calls or on throw statements but not on individual instructions. BTW: This has nothing to do with networking. Follow up is set to the vc.language group. -- Steve Maillet (eMVP) Entelechy Consulting smaillet_AT_EntelechyConsulting_DOT_com |
![]() |
| Tags |
| exceptions, question |
| Thread Tools | |
| Display Modes | |
|
|