This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: exceptions in C++
- From: Matthias Oltmanns <Mathias dot Oltmanns dot Oltmanns at sysde dot eads dot net>
- To: eljay at adobe dot com, gcc-help at gcc dot gnu dot org
- Date: 26 Mar 2003 09:28:13 +0100
- Subject: Re: exceptions in C++
- References: <BAA5ED00.7DEE%eljay@adobe.com>
Hi John,
many good ideas but i'm dreaming of a more general solution. The divide
by zero was an example for situations, where the runtime-environment
signals errors by "signals" instead by exceptions. I would like to see
a general solution, where i'm able to deal only with exceptions. Other
situations are segmentation faults, bus erros etc.
Is it possible to catch these errors and to continue in the program flow
without the need of termination?
cu
Matthias
Am Die, 2003-03-25 um 18.24 schrieb John Love-Jensen:
> Hi Matthias,
>
> How about this...
>
> try {
> if(denominator == 0)
> throw DivideByZeroException();
> int a = numerator / denominator;
> } catch (DivideByZeroException err) {
> ...
> }
>
> Or this macro-magic function hiding...
>
> #define DIV(_r, _n, _d) \
> if(_d == 0) throw DivideByZeroException(); \
> _r = _n / _d;
>
> Or this preferred inline equivalent...
>
> template <typename T>
> inline T Divide(T n, T d) {
> if(d == 0) throw DivideByZeroException();
> return n / d;
> }
>
> > As an example by providing a signal handler which is capable to raise
> exceptions.
>
> I believe you cannot throw exceptions through a signal handler, because
> you'd be throwing an exception across a C-barrier.
>
> --Eljay
--
Matthias Oltmanns
Tel: 04421-1543-274
mail: Mathias dot Oltmanns dot Oltmanns at sysde dot eads dot net