Topic: Exceptions in C++


Author: David R Tribble <david.tribble@noSPAM.central.beasys.com>
Date: 1998/08/01
Raw View
Maria Motta wrote:
> I'm programming C++ making porting of Ada code.
> In Ada there is a predefined exeption, "CONSTRAINT ERROR", which
> includes errors like the attempt to use a record component which does
> not exist for the current discriminant values; upon an attempt to use
> a selected component, an indexed component, a slice, or an attribute,
> of an object designated by an access value, if the object does not
> exist because the access value is null.
>
> My question is: do we have something similar in C++? How can I handle
> an exception like this?

If I remember my Ada correctly, what you're asking, in C++ lingo,
is: Is there an exception like Ada's CONSTRAINT ERROR which is
raised whenever an attempt is made to access a struct member when
the accessing pointer is null?

Translations (I think):
    Ada              C++
    ---------------  -------
    access variable  pointer
    record           struct
    component        member

The answer, unfortunately, is "no".  C++ does very little runtime
checking of pointer accesses.  The usual result of accessing
anything through a null pointer is variously called a "memory
segmentation violation" (SEGV), or "core dump", or "general
protection fault", depending on your system.  In other words,
a (usually) fatal runtime error.

There was a related thread on this newsgroup recently about
trying to detect attempts to access member functions through a
null pointer.  The general consensus was that it might be possible,
but it's usually hard to do or at least not 100% effective, and
in any case most programmers don't do it.  And C++ doesn't do
it for you either.

-- David R. Tribble, dtribble@technologist.com --
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: James Kuyper <kuyper@wizard.net>
Date: 1998/07/29
Raw View
Maria Motta wrote:
>
> Hello,
> I'm programming C++ making porting of Ada code.
> In Ada there is a predefined exeption,  "CONSTRAINT ERROR", which includes
> errors like the attempt to use a record component which does not exist for
> the current discriminant values; upon an attempt to use a selected component,
> an indexed component, a slice, or an attribute, of an object designated by an
> access value, if the object does not exist because the access value is null.
>
> My question is: do we have something similar in C++? How can I handle an
> exception like this?
> Somebody has suggested to use the standard C++ class library, but where can I
> find it or get some documentation?

The newly approved C++ standard specifies some standard exception
classes which you are encouraged to derive from. It specifies some
particular exceptions that are throw by some of the standard library
functions. You can find more details by looking in the FAQ using the
link which appears at the bottom of every message on this newsgroup. No
single exception corresponds exactly to an Ada "CONSTRAINT ERROR", but
that's the kind of problem that is normal when porting code between
languages. The out_of_range exception in <stdexcept> seems the most
relevant one.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: AllanW@my-dejanews.com
Date: 1998/07/30
Raw View
In article <35BDED41.3BC321D5@pisa.intecs.it>,
  Maria Motta <mariam@pisa.intecs.it> wrote:
> Hello,
> I'm programming C++ making porting of Ada code.
> In Ada there is a predefined exeption,  "CONSTRAINT ERROR", which includes
> errors like the attempt to use a record component which does not exist for
> the current discriminant values; upon an attempt to use a selected component,
> an indexed component, a slice, or an attribute, of an object designated by an
> access value, if the object does not exist because the access value is null.
>
> My question is: do we have something similar in C++? How can I handle an
> exception like this?
> Somebody has suggested to use the standard C++ class library, but where can I
> find it or get some documentation?

I'm not an expert in Ada, but my understanding of a Constraint Error is
that the function was called in an inappropriate way.  This suggests
that a programming error has been detected, because it was incorrect for
the caller to even call this function in this way.  Is that accurate?

If so, please look up "assert" in the C++ run-time library. This should
fit your requirement quite nicely, with the added bonus that if you want
to, you can turn off the checks for your final build, once you are
reasonably sure that the Constraint Errors never happen.  (However, note
that some C++ programmers prefer to leave them on, because the improved
diagnostics make it easier to solve problems in the field).

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Maria Motta <mariam@pisa.intecs.it>
Date: 1998/07/29
Raw View
Hello,
I'm programming C++ making porting of Ada code.
In Ada there is a predefined exeption,  "CONSTRAINT ERROR", which includes
errors like the attempt to use a record component which does not exist for
the current discriminant values; upon an attempt to use a selected component,
an indexed component, a slice, or an attribute, of an object designated by an
access value, if the object does not exist because the access value is null.

My question is: do we have something similar in C++? How can I handle an
exception like this?
Somebody has suggested to use the standard C++ class library, but where can I
find it or get some documentation?

Thanks in advance,
    Maria
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]