handling exceptions

A

annex

hi, i have some queries about handling exceptions, i'm using Borland c++
Builder 6.

1) what's the best way to catch multiple exceptions in one catch statement?
is this possible? for e.g i want to catch 2 exceptions; MyEx1 and MyEx2,
both to be handled in the same way. how to do this?

try
{
doSomething();
}
catch (MyEx1)
{
sameCode();
}
catch (MyEx2)
{
sameCode();
}

note that both exceptions are to be handled using the same piece of code.

2) what's the difference btw catch(...) and catch(Exception). will
catch(Exception) catch all unhandled exceptions also like does catch(...) ?

try
{
doSomething();
}
catch(...)
{
handle();
}

and

try
{
doSomething();
}
catch(Exception)
{
handle();
}
 
S

Siemel Naran

1) what's the best way to catch multiple exceptions in one catch statement?
is this possible? for e.g i want to catch 2 exceptions; MyEx1 and MyEx2,
both to be handled in the same way. how to do this?

try
{
doSomething();
}
catch (MyEx1)
{
sameCode();
}
catch (MyEx2)
{
sameCode();
}

note that both exceptions are to be handled using the same piece of code.

See if you can derive MyEx1 and MyEx2 from MyEx. Then catch a MyEx,
preferrably by reference. With this one catch statement you catch all
exceptions derived from MyEx, like MyEx1 and MyEx2. If catching by
reference, feel free to call virtual functions of the caught MyEx object,
and also to throw the exception with "throw;".

There is no syntax

catch (MyEx2 || MyEx2)

The use of goto seems ugly, and I'm not sure if is even allowed.

2) what's the difference btw catch(...) and catch(Exception). will
catch(Exception) catch all unhandled exceptions also like does catch(...)
?

catch (Exception) or catch(const Exception&) catch exceptions of type
Exception or derived from Exception. You can name the exception object as
in catch(const Exception& e) and then call virtual functions on the object,
such as e.what(). You can rethrow the exception object.

catch (...) catches all exceptions. You can't call virtual functions on the
object. About all you can say is "unhandled exception" and optionally
rethrow the exception object.
 
R

Rolf Magnus

annex said:
hi, i have some queries about handling exceptions, i'm using Borland
c++ Builder 6.

1) what's the best way to catch multiple exceptions in one catch
statement? is this possible? for e.g i want to catch 2 exceptions;
MyEx1 and MyEx2, both to be handled in the same way. how to do this?

try
{
doSomething();
}
catch (MyEx1)
{
sameCode();
}
catch (MyEx2)
{
sameCode();
}

note that both exceptions are to be handled using the same piece of
code.

The only way to do that is by deriving MyEx1 and MyEx2 from a common
base class and catching this base class.
2) what's the difference btw catch(...) and catch(Exception).

The former catches all exceptions, but you don't get the exception
object. The latter catches only objects of type 'Exception'.
Btw, you should always catch by reference.
 
A

Alf P. Steinbach

* Rolf Magnus:
The only way to do that is by deriving MyEx1 and MyEx2 from a common
base class and catching this base class.

Or you can use 'catch(...)'.

The former catches all exceptions, but you don't get the exception
object.

You do. To retrieve the passed-in exception object rethrow and catch
it. To rethrow it just use 'throw;'.
 
R

Roberto =?ISO-8859-15?Q?D=EDaz?=

annex wrote:

2) what's the difference btw catch(...) and catch(Exception). will
catch(Exception) catch all unhandled exceptions also like does catch(...)

The first will catch all exceptions derived from Exception the later will
catch all exceptions no matter wether they are derived or not from
Exception.

The best way to catch exceptions is to use its hierarchi:

class exception1 : pubic exception
{
};

class exception2 : public exception1
{
};

class exception3 : public exception2
{
};

try {
// ........ code here ....
} catch (exception3 &e3) { // this will catch e3
// .....
} catch (exception2 &e2) { // this will catch e2 and e3... supposing the
//previous catch was not there
// ......
} catch (exception1 &e1) { // this will catch e1, e2, e3... as before
// ......
} catch (exception &e) { // this will catch e, e1, e2, e3.. as before
// ......
} catch (...) { // this will catch everything
// ...
}


Regards

Roberto
 
R

Rolf Magnus

Alf said:
* Rolf Magnus:

[example snipped]
Or you can use 'catch(...)'.

Of course, but then you will not only catch MyEx1 and MyEx2, but also
every other exception, and you can't access the exception object. The
OP didn't make clear whether that would matter or not.
You do. To retrieve the passed-in exception object rethrow and catch
it. To rethrow it just use 'throw;'.

I don't see what that would be good for. If I want to catch class
Exception, then I do so. Why should I first catch(...), rethrow and
then catch it again by class name?
 
A

Alf P. Steinbach

* Rolf Magnus:
Alf said:
* Rolf Magnus:

[example snipped]
Or you can use 'catch(...)'.

Of course, but then you will not only catch MyEx1 and MyEx2, but also
every other exception

Yes, and so?

and you can't access the exception object.

That is incorrect.

I don't see what that would be good for. If I want to catch class
Exception, then I do so. Why should I first catch(...), rethrow and
then catch it again by class name?

To avoid catching a number of different types a number of different
places, which just might be the OP's actual problem.
 
R

Rolf Magnus

Alf said:
Yes, and so?

The OP asked how to catch multiple exceptions, not how to catch all of
them.
That is incorrect.

Then how would you access it within the catch(...)?
To avoid catching a number of different types a number of different
places, which just might be the OP's actual problem.

I still don't see what that would be good for. Could you provide an
example?
 
R

Rob Williscroft

Rolf Magnus wrote in in
comp.lang.c++:
I still don't see what that would be good for. Could you provide an

Its good for avoiding goto (ducks:).

/* should only be called within a catch block
*/
void myhandler()
{
try
{
throw;
}
catch ( MyEx1 const & )
{
}
catch ( MyEx2 const & )
{
}
//handle both cases ...
}


void f()
{
try
{
// something that throws
}
catch ( ... )
{
myhandler();
}
}

I've never used it, or seen it used (except in NG posts).

You can of course wrap myhandler() up in another "handler"
function that handles different exceptions etc etc.

Rob.
 
A

annex

Rolf Magnus said:
annex wrote:

[snipped]
2) what's the difference btw catch(...) and catch(Exception).

The former catches all exceptions, but you don't get the exception
object. The latter catches only objects of type 'Exception'.
Btw, you should always catch by reference.
will catch(Exception) catch all unhandled exceptions also like does
catch(...) ?

No.

Why not? aren't all exceptions derived from the base class Exception (in
BCB)? if so, then won't catching (Exception& e) catch all exceptions same as
catching (...) ? the only difference is by catching (Exception& e) we can
access the exception object directly.

Cheers.
 
O

Owen Jacobson

Why not? aren't all exceptions derived from the base class Exception (in
BCB)? if so, then won't catching (Exception& e) catch all exceptions same as
catching (...) ? the only difference is by catching (Exception& e) we can
access the exception object directly.

All exceptions in the C++ standard are inherited from std::exception, but
there is no guarantee that library writers will derive their exceptions
from anything at all. It is perfectly legitimate to throw an intrinsic
type (int, char *, etc.), which isn't an object at all.
 
R

Rolf Magnus

annex said:
Why not? aren't all exceptions derived from the base class Exception
(in BCB)?

I have no idea about BCB (isn't this some Borland compiler?). Anyway,
the standard C++ exceptions are derived from std::exception and nothing
else. Actually, I thought that by "Exception", you meant your own base
class for exceptions. You can derive your own exceptions from whatever
you want or you can even throw an int. Those are all not caught if you
only catch Exception. So if Exception is the base class for some
Borland specific exceptions, you will only catch those ones and no
others.
 
S

Siemel Naran

I have no idea about BCB (isn't this some Borland compiler?). Anyway,
the standard C++ exceptions are derived from std::exception and nothing
else. Actually, I thought that by "Exception", you meant your own base
class for exceptions. You can derive your own exceptions from whatever
you want or you can even throw an int. Those are all not caught if you
only catch Exception. So if Exception is the base class for some
Borland specific exceptions, you will only catch those ones and no
others.

The Borland Exception class is not derived from std::exception in any way,
only from TObject. But it's a good case for multiple inheritance as
std::exception is abstract (as what() const = 0 is pure virtual). They
should derive Exception from TObject and std::exception.

Anyway, it basically means to catch all exceptions you should to

catch (Sysutils::Exception& e) { }
catch (std::exception& e) { }
catch (...) { /*unknown exception */ }

If deriving your own generic exception base class from Sysutils::Exception,
consider using multiple inheritance to derive from both Sysutils::Exception
and std::exception if you want to be able to treat your exceptions as either
Borland or standard exceptions. Of course, instead of std::exception, you
could derive from std::runtime_error or the others.
 
X

Xiaobin Yang

will catch(Exception) catch all unhandled exceptions also like does
you can define your own exception class MyException and throw it out. In
this case, MyException is not derived from Exception.

class MyException {};
class MyClass {
public:
void Function() {
....
throw MyException();
}

int main(){

try {
MyClass o; o.Function();
}
catch(Exception) { cout << "Exception\n"; }
catch(MyException) {cout << "MyException\n";}

}
 
A

annex

you can define your own exception class MyException and throw it out. In
this case, MyException is not derived from Exception.

class MyException {};
class MyClass {
public:
void Function() {
....

This is interesting, but if i were to define my own exception class not
deriving from any base exception class, should the new exception class carry
certain properties or functions that will enable it to be a valid exception
class? if yes, what are the prerequisites for a class to be a valid
exception class e.g. Message property perhaps?
 
A

annex

All exceptions in the C++ standard are inherited from std::exception, but
there is no guarantee that library writers will derive their exceptions
from anything at all. It is perfectly legitimate to throw an intrinsic
type (int, char *, etc.), which isn't an object at all.

yeah i've read this somewhere but didn't quite understand. when, where and
why would we want to throw an intrinsic type exception e.g int? pls give a
simple example how to use it. cheers.
 
T

Tim Orbaker

annex said:
This is interesting, but if i were to define my own exception class not
deriving from any base exception class, should the new exception class carry
certain properties or functions that will enable it to be a valid exception
class? if yes, what are the prerequisites for a class to be a valid
exception class e.g. Message property perhaps?

In C++ you can throw any value. It can be a constant number, a POD type
or a class instance. C++ imposes very few restrictions on exceptions.

What makes an exception valid is a matter of your application. Your
design choices will determine what is useful and appropriate.

Personally, I prefer to use exceptions with: the bare minimum necessary
to generate a meaningful error message; and as much cleanup code as
possible (it makes for cleaner catch blocks and less duplication of code).

Tim
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

annex said:
yeah i've read this somewhere but didn't quite understand. when, where and
why would we want to throw an intrinsic type exception e.g int? pls give a
simple example how to use it. cheers.

When doing a very small test program.
 
R

red floyd

annex said:
yeah i've read this somewhere but didn't quite understand. when, where and
why would we want to throw an intrinsic type exception e.g int? pls give a
simple example how to use it. cheers.

Quick and dirty test code.

#include <iostream>
void throws_an_int()
{
throw 1;
}

int main(int, char *[])
{
try
{
throws_an_int();
std::cout << "no throw!" << std::endl;
}
catch (int x)
{
std::cout << "caught int exception value " << x << std::endl;
}
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top