Networking Forums  

Go Back   Networking Forums > Networking Newsgroups > Windows ME Networking

C++ Exceptions Question

Reply
 
Thread Tools Display Modes
  #1  
Old 11-21-2003, 12:53 AM
Default C++ Exceptions Question



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
Reply With Quote
  #2  
Old 11-21-2003, 04:00 PM
Arkady Frenkel
Guest
 
Posts: n/a
Default Re: C++ Exceptions Question

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
>
>



Reply With Quote
  #3  
Old 11-21-2003, 04:54 PM
LiquidJ
Guest
 
Posts: n/a
Default Re: C++ Exceptions Question

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
>
>



Reply With Quote
  #4  
Old 11-23-2003, 04:17 PM
Steve Maillet \(eMVP\)
Guest
 
Posts: n/a
Default Re: C++ Exceptions Question

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


Reply With Quote
Reply

Tags
exceptions, question

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT. The time now is 05:16 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.