Howto catch Win32 exceptions in C++? 
Author Message
 Howto catch Win32 exceptions in C++?

I'm getting a system exception in my Win32 C++ code.  This is not MFC, just
plain Win32.  I can catch the exception with 'catch(...)', but how do I
interpret it?  Where do I find the exception codes and how do I catch them?

Thanks,
Tom.



Fri, 18 Jun 2004 12:34:57 GMT  
 Howto catch Win32 exceptions in C++?
do this
try
{
    // Do something to throw a file exception.
Quote:
}

catch( CFileException* theException )
{
    if( theException->m_cause == CFileException::fileNotFound )
        TRACE( "File not found\n" );
    theException->Delete();

Quote:
}



Quote:
> I'm getting a system exception in my Win32 C++ code.  This is not MFC,
just
> plain Win32.  I can catch the exception with 'catch(...)', but how do I
> interpret it?  Where do I find the exception codes and how do I catch
them?

> Thanks,
> Tom.



Fri, 18 Jun 2004 14:04:06 GMT  
 Howto catch Win32 exceptions in C++?
try/catch is C++ exceptions ( opposite TRY/CATCH - MFC exceptions )
_try/_except is Win32 exceptions.
Arkady


Quote:
> do this
> try
> {
>     // Do something to throw a file exception.
> }
> catch( CFileException* theException )
> {
>     if( theException->m_cause == CFileException::fileNotFound )
>         TRACE( "File not found\n" );
>     theException->Delete();
> }



> > I'm getting a system exception in my Win32 C++ code.  This is not MFC,
> just
> > plain Win32.  I can catch the exception with 'catch(...)', but how do I
> > interpret it?  Where do I find the exception codes and how do I catch
> them?

> > Thanks,
> > Tom.



Fri, 18 Jun 2004 14:49:26 GMT  
 Howto catch Win32 exceptions in C++?


Quote:


> > I'm getting a system exception in my Win32 C++ code.  This is not MFC,
> just
> > plain Win32.  I can catch the exception with 'catch(...)', but how do I
> > interpret it?  Where do I find the exception codes and how do I catch
> them?

> The short answer to your question: check the docs for _set_se_translator()
> which allows you to define a translator function that catches structured
> exceptions and throws C++ exceptions.

> If you need help in doing that you might want to take a look at the
> Win32Exception class and test program which are attached.

Thanks.  I took a look at your code, but I think that something is already
doing that for me, since I'm able to catch exceptions with the 'catch(...)'
statement.

I realize now that what I wrote in my original post is wrong: it is not
plain Win32 code, it is ATL code.  I can catch ATL exceptions with
catch(_com_error &e), but I want to catch system exceptions.  I'm wondering
what goes in my catch statement.

Tom.



Fri, 18 Jun 2004 23:53:30 GMT  
 Howto catch Win32 exceptions in C++?

Quote:
> Thanks.

You are welcome.

Quote:
> I took a look at your code, but I think that something is already
> doing that for me, since I'm able to catch exceptions with the
'catch(...)'
> statement.

Perhaps my friend Doug will chime in here. If I understand what he has
posted here in the past, catching structured exceptions inside a catch (...)
is a defect on the part of VC/C++.

The code I posted uses _set_se_translator() ( set structured exception
translator ) which is documented to catch structured exceptions defined by
Win32 and capture  what the o/s knows about the exception. The translator is
supposed to construct an instance of  C++ exception and throw that. That
done, handling a "fixed up" SE is not any different from handling any other
C++ exception.

The only thing noteworthy about the class I posted is that it shows how to
dechiper the cause and location of the exception. As far as I know, C++
offers no portable way to get that information from any old exception.

Regards,
Will



Sat, 19 Jun 2004 06:06:15 GMT  
 Howto catch Win32 exceptions in C++?



Quote:


> Perhaps my friend Doug will chime in here. If I understand what he has
> posted here in the past, catching structured exceptions inside a catch
(...)
> is a defect on the part of VC/C++.

It's documented, intentional behavior (AFAIK), which some people consider to
be a bug and others consider to be wonderful and revolutionary.

Quote:
> The code I posted uses _set_se_translator()

_set_se_translator is of course the right way to deal with this situation.
Btw nice little class :)  The only thing I'd put in mine that you didn't
have is code to capture a stack trace (messy and non-portable but darn
handy).

-cd



Sat, 19 Jun 2004 08:15:25 GMT  
 Howto catch Win32 exceptions in C++?
Hi Carl,

Quote:
> It's documented, intentional behavior (AFAIK), which some people consider
to
> be a bug and others consider to be wonderful and revolutionary.

Well, if anyone one on the VC++ or PSS teams at MS ever said that, I would
be happy to pester them to death until they take their words back <vbg>.

It _is_ a bug, no doubt about that. It is not only inconsistent, but also
pretty much against the C++ rules, and tends to only work when the moon is
in the right position and you're using debug builds.

--
Tomas Restrepo



Sat, 19 Jun 2004 12:15:38 GMT  
 Howto catch Win32 exceptions in C++?


Quote:
> Hi Carl,

> > It's documented, intentional behavior (AFAIK), which some people
consider
> to
> > be a bug and others consider to be wonderful and revolutionary.

> Well, if anyone one on the VC++ or PSS teams at MS ever said that, I would
> be happy to pester them to death until they take their words back <vbg>.

> It _is_ a bug, no doubt about that. It is not only inconsistent, but also
> pretty much against the C++ rules, and tends to only work when the moon is
> in the right position and you're using debug builds.

OR compiled your code with /EHa

-cd



Sat, 19 Jun 2004 17:18:42 GMT  
 Howto catch Win32 exceptions in C++?
Hi Carl,

Quote:
> OR compiled your code with /EHa

That's the idea, but, fwiw, I've seen some ugly bugs rearing their ugly
heads because it was not happening with certain very specific escenarios.
You can imagine the fun in debugging that!

In general, I wouldn't recomend relying on it in any case, and using
_set_se_translator to be on the safe side.

--
Tomas Restrepo



Sat, 19 Jun 2004 23:08:08 GMT  
 Howto catch Win32 exceptions in C++?

Quote:
> Hi Carl,

> > OR compiled your code with /EHa

> That's the idea, but, fwiw, I've seen some ugly bugs rearing their ugly
> heads because it was not happening with certain very specific escenarios.
> You can imagine the fun in debugging that!

> In general, I wouldn't recomend relying on it in any case, and using
> _set_se_translator to be on the safe side.

Likewise, absolutely.  Using the translator you can capture such wonderfully
complete and helpful infomration about Win32 exceptions I'd never leave home
without it.

My most recent web-server-based project was a large JSP/C++ project.  We
caught all SEH exceptions, as well as all C++ exceptions and reflected them
back into the Java domain, mapping the C++ exception hierarchy to a parallel
Java exception hierarchy.  The result was stupendous for debugging: if an
access violation occurred, for example, we'd have a stack trace down through
the Java code, then down through the C++ code (which function names & line
numbers if it a .DBG file was available) right down to the point of the
error.  Made the choice easy when we discovered a crashing bug whether to
debug it under Windows or Linsux.

-cd



Sun, 20 Jun 2004 01:12:08 GMT  
 Howto catch Win32 exceptions in C++?

Quote:
> It _is_ a bug, no doubt about that. It is not only inconsistent, but
also
> pretty much against the C++ rules

I don't think it is against "C++ rules", strictly speaking. I guess by
the time SE happens, the program has already invoked undefined behavior
under C++ standard (like, the program divided by zero or overran memory
buffer or something) and the standard allows absolutely anything to
happen in this case.
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken



Mon, 21 Jun 2004 00:21:22 GMT  
 Howto catch Win32 exceptions in C++?
Hi Igor,

Quote:
> I don't think it is against "C++ rules", strictly speaking. I guess by
> the time SE happens, the program has already invoked undefined behavior
> under C++ standard (like, the program divided by zero or overran memory
> buffer or something) and the standard allows absolutely anything to
> happen in this case.

That's true, my wording was a little too strong, I guess. I do believe this
is not exactly what the C++ spec designers intended to happen, though...

--
Tomas Restrepo



Mon, 21 Jun 2004 01:22:51 GMT  
 Howto catch Win32 exceptions in C++?

Quote:
> Hi Igor,

> > I don't think it is against "C++ rules", strictly speaking. I guess
by
> > the time SE happens, the program has already invoked undefined
behavior
> > under C++ standard (like, the program divided by zero or overran
memory
> > buffer or something) and the standard allows absolutely anything to
> > happen in this case.

> That's true, my wording was a little too strong, I guess. I do believe
this
> is not exactly what the C++ spec designers intended to happen,

though...

SEH is the extension of the language not covered by C++ spec. This
particular design decision, good or bad, is on Microsoft's hands. Again,
SE is not even supposed to happen in a compliant program.
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken



Mon, 21 Jun 2004 03:26:21 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. Catching Win32 exceptions with a C++ catch-block

2. catching Win32 API exceptions in C++

3. Catching hardware exceptions using C++ exceptions

4. Catching SEC Exceptions as C++ Exceptions

5. HOWTO catch exception from 16-bit DLL

6. HOWTO catch exception from 16-bit DLL

7. Win32 structured exceptions vs C++ exceptions

8. Catching Win32/MFC Exceptions in eVC++

9. catching math exceptions with C++

10. Catching SEH and c++ exception

11. Q: Why try catch cannot catch the exception

12. C++/MFC and exceptions- catch(...)=bad?

 

 
Powered by phpBB® Forum Software