HELP ??try and catch 
Author Message
 HELP ??try and catch

hello all
I am using VC++ 6 service pack3 on Win2k. Could someone enlighten me about
"try" and "catch".?

I got "Catch" from the following code
/////////////////////////////////////////////////////
#include <stdio.h>
int main()
{
  try
  {
    return (*((unsigned char*)0) = 'g');
  }
  catch(...)
  {
    printf("Catch\n");
    return 1;
  }
  return 0;

Quote:
}

but this one, I got GPF, or sometime nothing at all.
///////////////////////////
#include <stdio.h>

int main()
{
  int a[100];

  try
  {
    return  a[102];
  }
  catch(...)
  {
    printf("Catch\n");
    return 1;
  }
  return 0;

Quote:
}



Tue, 07 Jan 2003 03:00:00 GMT  
 HELP ??try and catch


Quote:
> hello all
> I am using VC++ 6 service pack3 on Win2k. Could someone enlighten me about
> "try" and "catch".?

> I got "Catch" from the following code
> /////////////////////////////////////////////////////
> #include <stdio.h>
> int main()
> {
>   try
>   {
>     return (*((unsigned char*)0) = 'g');
>   }
>   catch(...)
>   {
>     printf("Catch\n");
>     return 1;
>   }
>   return 0;
> }

In this sample you tried write to address in restricted area, it is used for
catching operation with NULL pointers (first 64k (0 - 0xffff) ). Every try
to read or write from this area will cause exception.

- Show quoted text -

Quote:

> but this one, I got GPF, or sometime nothing at all.
> ///////////////////////////
> #include <stdio.h>

> int main()
> {
>   int a[100];

>   try
>   {
>     return  a[102];
>   }
>   catch(...)
>   {
>     printf("Catch\n");
>     return 1;
>   }
>   return 0;
> }

In this sample you read from memory which is not restricted, it is used by
your application. VC++ doesn't check bounds of array you allocate, you must
care about it. a[102] is just "int" in your memory space, this will not
cause exception.


Tue, 07 Jan 2003 03:00:00 GMT  
 HELP ??try and catch

Quote:

>hello all
>I am using VC++ 6 service pack3 on Win2k. Could someone enlighten me about
>"try" and "catch".?

>I got "Catch" from the following code
>/////////////////////////////////////////////////////
>#include <stdio.h>
>int main()
>{
>  try
>  {
>    return (*((unsigned char*)0) = 'g');
>  }
>  catch(...)
>  {
>    printf("Catch\n");
>    return 1;
>  }
>  return 0;
>}

It's a bug that catch(...) can sometimes catch untranslated Win32
structured exceptions. You should never rely on this.

- Show quoted text -

Quote:
>but this one, I got GPF, or sometime nothing at all.
>///////////////////////////
>#include <stdio.h>

>int main()
>{
>  int a[100];

>  try
>  {
>    return  a[102];
>  }
>  catch(...)
>  {
>    printf("Catch\n");
>    return 1;
>  }
>  return 0;
>}

a+102 exists in your address space, so the hardware doesn't detect a
problem, and the compiler doesn't check for out of bounds array
access. Any C++ compiler that does that should provide a way to turn
it off.

--
Doug Harrison [VC++ MVP]
Eluent Software, LLC
http://www.eluent.com
Useful tools for Visual C++ and Windows



Tue, 07 Jan 2003 03:00:00 GMT  
 HELP ??try and catch
While I agree that it is dangerous to rely catch(...), especially in
the face of synchronous vs asynchronous exceptions, it is not a bug
that catch(...) will catch structured exceptions.  It is an
intentionally designed feature of the compiler.

Joe O'.

"Doug Harrison [MVP]" wrote

Quote:
> It's a bug that catch(...) can sometimes catch untranslated Win32
> structured exceptions. You should never rely on this.



Tue, 07 Jan 2003 03:00:00 GMT  
 HELP ??try and catch

Quote:

>While I agree that it is dangerous to rely catch(...), especially in
>the face of synchronous vs asynchronous exceptions, it is not a bug
>that catch(...) will catch structured exceptions.  It is an
>intentionally designed feature of the compiler.

Well, I guess you could call it a "design mistake". It's something
that needs to be fixed, and I think it's fair to call it a bug,
because the effect is that of a bug, irrespective of any good
intentions.

--
Doug Harrison [VC++ MVP]
Eluent Software, LLC
http://www.eluent.com
Useful tools for Visual C++ and Windows



Tue, 07 Jan 2003 03:00:00 GMT  
 HELP ??try and catch
Joe,

Quote:
> While I agree that it is dangerous to rely catch(...), especially in
> the face of synchronous vs asynchronous exceptions, it is not a bug
> that catch(...) will catch structured exceptions.  It is an
> intentionally designed feature of the compiler.

Might be, and I'd be OK (sort of) with it if it actually worked like it is
supposed to, except, well, it doesn't.

I'm with Doug here. It's a bug and it should be fixed; the sooner the
better.
It's a shame, given that SEH is actually a pretty neat way of dealing with
some errors, but is currently almost unusable from C++ code, and quite
frankly, I rarely write pure C these days :(

--
Tomas Restrepo
http://www.mvps.org/windev



Tue, 07 Jan 2003 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. TRY-CATCH vs. try-catch

2. try/catch VS TRY/CATCH

3. Can you catch CDBExection with try-catch keyword?

4. why {try catch} does not catch

5. Q: Why try catch cannot catch the exception

6. HELP! Basic Try/Catch problem

7. Help on C++ try/catch/finally

8. HELP! Try/catch problem

9. try...catch newbie, please help

10. Try catch blocks - can multiple methods share a single try/catch combination?

11. Exception Handling and CDatabase / CRecordset: try-catch( CDBException cdbe) vs. try-catch (...)

12. Try catch blocks - can multiple methods share a single try/catch combination?

 

 
Powered by phpBB® Forum Software