From: Sada on
hi all,
I want to handle invalid pointer operations in my application using
exception handling, in visual studio __try , __catch are used for SEH
( Structured Exception Handling) but how can I handle the same in GCC.

For example I want to catch EXC_BAD_ACCESS exception in my C++
application.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Balog Pal on
"Sada" <steggi.cs(a)gmail.com>

> I want to handle invalid pointer operations in my application using
> exception handling,

Invalid pointer op leads to UNDEFIEND BEHAVIOR. So you can't realisticly aim
to handle it within the language.

> in visual studio __try , __catch are used for SEH
> ( Structured Exception Handling)

Indeed some cases are picked up by catch(...) in some versions of VS (with
some switches), but I doubt there is any guarantee that every case is
caught.

> but how can I handle the same in GCC.
>
> For example I want to catch EXC_BAD_ACCESS exception in my C++
> application.

IMO forget the idea and stay on defined ground... You're chasing the foot
of the rainbow.



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: REH on
On Sep 8, 11:43 am, Sada <steggi...(a)gmail.com> wrote:
> hi all,
> I want to handle invalid pointer operations in my application using
> exception handling, in visual studio __try , __catch are used for SEH
> ( Structured Exception Handling) but how can I handle the same in GCC.
>
> For example I want to catch EXC_BAD_ACCESS exception in my C++
> application.
>

That depends on your compiler's implementation. On some systems,
invalid pointer accesses can be routed to a signal handler, but I
don't know a safe and/or standard way to route a signal to an
exception.

REH


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Timo Geusch on
Sada <steggi.cs(a)gmail.com> writes:

> hi all,
> I want to handle invalid pointer operations in my application using
> exception handling, in visual studio __try , __catch are used for SEH
> ( Structured Exception Handling) but how can I handle the same in GCC.

GCC on which OS? SEH is OS-specific inasmuch as it's only available in
Windows.

> For example I want to catch EXC_BAD_ACCESS exception in my C++
> application.

You might want to look into trapping the relevant signals if the
underlying OS supports these and then try to translate them into
exceptions. It's not going to be pretty and it's not going to emulate
the behaviour of SEH.

--
Timo Geusch
Codesmith Consulting Ltd
The lone C++ coder's blog: http://codeblog.bsdninjas.co.uk/

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Martin T. on
Balog Pal wrote:
> "Sada" <steggi.cs(a)gmail.com>
>
>> I want to handle invalid pointer operations in my application using
>> exception handling,
>
> Invalid pointer op leads to UNDEFIEND BEHAVIOR. So you can't realisticly aim
> to handle it within the language.
>

You cannot handle it within the standardized language. However,
undefined behaviour may well be well defined on a subset of implementations.

>> in visual studio __try , __catch are used for SEH
>> ( Structured Exception Handling)
>
> Indeed some cases are picked up by catch(...) in some versions of VS (with
> some switches), but I doubt there is any guarantee that every case is
> caught.
>

The OP said __try and __catch which are the MS constructs for catching
SEH. You can still use these if you (probably wisely) disable for
catch(...) to catch SEH exceptions.

>> but how can I handle the same in GCC.
>>
>> For example I want to catch EXC_BAD_ACCESS exception in my C++
>> application.
>
> IMO forget the idea and stay on defined ground... You're chasing the foot
> of the rainbow.
>

IMHO in daily practise there are just cases where trying to react to
invalid access is necessary.
So I guess the real question - probably better belonging to a gcc group
- is: what mechanisms does GCC with C++ provide to react to access
violations on Windows and/or on *nix?

That said, yes I agree that most of the time the best way to react will
be writing a dump file and terminating the application.

cheers,
Martin

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]