Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

try catch VS __try __except()

1,895 views
Skip to first unread message

sachin

unread,
Jan 28, 2009, 2:50:06 AM1/28/09
to
hi
can we catch exception which normally terminate program in C++
try catch block ?

currently i am catching it using __try __except
but this has few limitations and i want to catch those exceptions in C++
catch handler

thanks in advance

Alex Blekhman

unread,
Jan 28, 2009, 3:38:09 AM1/28/09
to
"sachin" wrote:
> can we catch exception which normally terminate program in C++
> try catch block ?

You can, but it requires some preparations. By default C++
try-catch block won't handle SEH exceptions.

> currently i am catching it using __try __except
> but this has few limitations and i want to catch those
> exceptions in C++ catch handler

Read this FAQ, it explains the difference between C++ nad SEH
exceptions and how to handle both types correctly:

"A Visual C++ Exception FAQ"
http://members.cox.net/doug_web/eh.htm

HTH
Alex


sachin

unread,
Jan 28, 2009, 5:26:06 AM1/28/09
to

hi Alex
The document was Good it helped me to understand the difference between C++
and C Exception handling..

but i am still confused about the best way to catch the SE in C++ To be
more specific @ what i want

1. I Agree that Structured Exceptions represent bugs in the program. But
the code is very huge and i hardly have time to run through all Line of code
and check for programming bug :) ..
2. The Tool crashes in live environment and causes panic @ client site so
as a short term fix what i want to do is catch those SE in the code and
handle those exception set of documents move them in separate directory and
process next document this way the tool will not stop even though there is
Win32 SE

i have written __try and __except block in main function but i want it
closer to a processing function so that the tool wont stop after exception
and i can restart the function ..

but when i tried putting __try __Except around the function compiler gives
me error
C2712: Cannot use __try in functions that require object unwinding

any idea @ ideal solution in this case ?

Ulrich Eckhardt

unread,
Jan 28, 2009, 6:02:43 AM1/28/09
to
sachin wrote:
> The document was Good it helped me to understand the difference between
> C++ and C Exception handling..

C doesn't have exceptions. What you probably mean is win32 SEH, which is
available as a compiler extension using __try/__except.

> but i am still confused about the best way to catch the SE in C++ To be
> more specific @ what i want
>
> 1. I Agree that Structured Exceptions represent bugs in the program. But
> the code is very huge and i hardly have time to run through all Line of
> code and check for programming bug :) ..

SEs don't necessarily represent bugs. Also, ignoring them isn't likely to
make your program run any better.

> 2. The Tool crashes in live environment and causes panic @ client site so
> as a short term fix what i want to do is catch those SE in the code and
> handle those exception set of documents move them in separate directory
> and process next document this way the tool will not stop even though
> there is Win32 SE
>
> i have written __try and __except block in main function but i want it
> closer to a processing function so that the tool wont stop after exception
> and i can restart the function ..

What you could do is

try {
...
} catch(std::exception const& e) {
log("exception: %1", e.what());
...
} catch(...) {
log("unknown exception);
...
}

In any case, there is no guarantee that you can recover from what the SE is
signalling. It could be that it is thrown due to a corrupted stack or
similar things. Just pretending it didn't happen and continuing won't get
you far.

Uli

--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

Alex Blekhman

unread,
Jan 28, 2009, 6:34:49 AM1/28/09
to
"sachin" wrote:
> i have written __try and __except block in main function but i
> want it closer to a processing function so that the tool wont
> stop after exception and i can restart the function ..

Then you should build your program with /EHa compiler switch
(asynchronous model), it will allow you to catch SEH exceptions in
catch(...) clause. Also, you may establish translation for SEH
exceptions to C++ exceptions with `_set_se_translator' call. See
short example here:

"Exception Handling Differences"
http://msdn.microsoft.com/en-us/library/de5awhsw.aspx

HTH
Alex


sachin

unread,
Jan 28, 2009, 7:08:00 AM1/28/09
to

hi Ulrich Eckhardt
will this try catch block handle Win32 SE ? with compiler flag set to Esa

basically i am interested to catch buffer overflow , NULL pointer referencing
and print stack of the line which caused this as a part exception handling
routine
this will help me to trace the bug .

Ulrich Eckhardt

unread,
Jan 28, 2009, 7:33:27 AM1/28/09
to
sachin wrote:
> hi Ulrich Eckhardt
> will this try catch block handle Win32 SE ? with compiler flag set to Esa

Oh, yes, you will have to tell the compiler that you want SEs in C++ catch
blocks.

> basically i am interested to catch buffer overflow , NULL pointer
> referencing and print stack of the line which caused this as a part
> exception handling routine this will help me to trace the bug .

Oh, getting out that information is a bit more tricky. I don't have a link
here, but search for "minidump", which should allow you to preserve some
information.

good luck!

sachin

unread,
Jan 28, 2009, 8:31:00 AM1/28/09
to
thanks Ulrich
but as per the document shared by alex
http://members.cox.net/doug_web/eh.htm
not all exception can be caught with compiler flag set to handle win 32 SE
and Catch(...)

..

sachin

unread,
Jan 28, 2009, 8:37:02 AM1/28/09
to

i have observed that
it is catching buffer overflow / null pointer reference exception .
i am still getting crash windows for such error.
can we disable default crash handler for DEBUG assertion and set our own

Alex Blekhman

unread,
Jan 28, 2009, 9:29:49 AM1/28/09
to
"sachin" wrote:
> i have observed that
> it is catching buffer overflow / null pointer reference
> exception .
> i am still getting crash windows for such error.
> can we disable default crash handler for DEBUG assertion and set
> our own

Be more specific. What do you mean by "crash windows"? Post here
small example that demonstrates the problem.

Alex


John Keenan

unread,
Jan 28, 2009, 11:31:54 AM1/28/09
to
sachin wriote:

> but i am still confused about the best way to catch the SE in C++

Instead of catching the structured exception directly I convert the
structured exception into a C++ exception using _set_se_translator then
catch the C++ exception. This is described in the MSDN article titled
"Handling Exceptions, Part 17". However I just searched msdn.microsoft.com
and can not find this article even though it is in my local MSDN library.
Perhaps others know how to search msdn.microsoft.com more effectively.

John


jjoohhnn

unread,
Jan 29, 2009, 2:02:29 AM1/29/09
to
Sachin,

Try with "SetUnhandledExceptionFilter".

Regards,
John.

"sachin" <sac...@discussions.microsoft.com> wrote in message
news:F6C7F5F3-8C5D-4655...@microsoft.com...

sachin

unread,
Jan 29, 2009, 5:49:01 AM1/29/09
to
thanks all it was informative thread for me
i have build Exception handling routine in my program
using _set_se_handler and /Eha flag now i can catch C++ Exception and
Win32_Exception :)

John i will try SetUnhandledExceptionFilter thanks

0 new messages