Mail Archives: djgpp/1996/09/03/14:33:17
On 27 Aug 96 at 18:35, Mihai Moise wrote:
> Michael Day wrote:
> > 2.0? I'm doing something like this:
> > try {
> > throw 10;
> > }
> > catch (int i) {
> >
> > }
> > catch (...) {
> >
> > }
> >
Just to add to the confusion:
The syntax above has nothing to do with UNIX style exceptions.
Instead it describes a new way of error handling introduced in C++
which are also know as software-exceptions and have been ( in think )
taken from ADA.
The idea is the following (and quite simple):
instead of having each function return defined values to indicate an
error or setting a variable like errno, a function when it encounters
an error "throw"s an exception. Such an exception can be any object
of any type, meaning it can contain user defined data apart from the
type definition.
As soon as the exception is throw, the program is aborted and resumed
at the next catch statement with an apropriate type.
an appropriate type would be the class of the thrown object or any
base class of this class.
Let's consider the following example (not compiled):
// class definitions
class Ex // this defines the bse exception class
{
public:
Ex() {};
~Ex() {};
};
class ExInt: public Ex // this defines a specialized exception
{ // class containing an int error number
public:
ExInt(int i)
{
err = i;
};
~ExInt() {};
int GetError() const
{
return err;
};
private:
int err;
};
class Test // the test class
{
public:
Test(){ptr = 0;};
~Test(){};
void SetPtr(void *p)
{
if (p == NULL)
throw ExInt(10); // if we get a NULL pointer we raise an
// exception
ptr = p;
};
protected:
void *ptr;
};
void main()
{
Test a;
try { // tell the compiler to watch for
// exceptions
a.SetPtr((void *)&a); // do something
a.SetPtr(NULL); // this is not allowed, an exception happens
}
catch(ExInt i) { // continue here if an ExInt exception
// happens, the object throw is called 'i'
// in this example
printf("Error: %d\n",i.GetError());
};
Test *b = NULL;
try {
int v = 0; // v gets destroyed when the exception
// happens
b = new Test;
b->SetPtr(NULL); // cause an exception
}
catch(Ex) { // we are not interested in the Error code
// this catches all exceptions derived from
// class Ex
printf("an error occured\n");
if (b != NULL) delete b;
}
catch (...) { // this catches all exceptions
// in this case it will not execute,
// as the Exception is caught by the previous
// catch block
if (b != NULL) delete b;
};
};
------ End Example ------
Actually exception handling can get quite complex, but when properly
used can minimize the amount "if (error) return error;" statements
often seen in C/C++ programs significantly and therefore make your
programs much more readable.
A few Caveats:
Throwing from a constructor/destructor is not allowed
I don't know if the GNU library does this, but when "new" runs
out of memory an exception gets thrown accoring to the draft
standard.
Signals won't get caught.
Be carefull of side effects when Objects on the stack get destroyed
due to an exception.
For a further discussion of this topic (which is quite off topic here)
see:
news:comp.std.c++
news:comp.lang.c++
book:The C++ Programming Language (Second Edition) by Bjarne Stoustrup
(Chapter "Exception Handling" page 293)
other:The C++ Standard Draft Working Paper
e.g.: http://www.bby.com.au/%7Egnb/wp/
mfg
Stefan Marte
......................................................................
. Stefan Marte: marte AT htu DOT tu-graz DOT ac DOT at .
......................................................................
"It is by caffeine alone I set my mind in motion,
It is by the beans of Java that thoughts acquire speed,
The hands acquire shaking, the shaking becomes a warning,
It is by caffeine alone I set my mind in motion."
- Raw text -