473,404 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

handling exceptions

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();
}

--------------
thanks ;-)

Jul 22 '05 #1
24 2234
"annex" <an*****@hotpop.com> wrote in message
news:40********@news.tm.net.my...
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.


Jul 22 '05 #2
annex wrote:
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.
will catch(Exception) catch all unhandled exceptions also like does
catch(...) ?
No.

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

and

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

--------------
thanks ;-)


Jul 22 '05 #3
* Rolf Magnus:
annex wrote:
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.


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

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


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;'.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #4
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
Jul 22 '05 #5
Alf P. Steinbach wrote:
* Rolf Magnus:
annex wrote:
> 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?
[example snipped]
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(...)'.


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.
> 2) what's the difference btw catch(...) and catch(Exception).


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;'.


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?

Jul 22 '05 #6
* Rolf Magnus:
Alf P. Steinbach wrote:
* Rolf Magnus:
annex wrote:

> 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?
[example snipped]
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(...)'.


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.


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;'.


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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #7
Alf P. Steinbach wrote:
>> 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(...)'.


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


Yes, and so?


The OP asked how to catch multiple exceptions, not how to catch all of
them.
and you can't access the exception object.


That is incorrect.


Then how would you access it within the 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;'.


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.


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

Jul 22 '05 #8
Rolf Magnus wrote in news:c9*************@news.t-online.com in
comp.lang.c++:
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


Its good for avoiding goto (ducks:).
example?


/* 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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #9

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:c9*************@news.t-online.com...
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.
Jul 22 '05 #10
On Sun, 06 Jun 2004 11:36:58 +0800, annex wrote:

Rolf Magnus wrote:
annex wrote:
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.


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.

--
Some say the Wired doesn't have political borders like the real world,
but there are far too many nonsense-spouting anarchists or idiots who
think that pranks are a revolution.

Jul 22 '05 #11
annex wrote:
> 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)?


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.

Jul 22 '05 #12
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:ca*************@news.t-
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.
Jul 22 '05 #13
> >> > 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)?


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";}

}

Jul 22 '05 #14
> 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?
Jul 22 '05 #15


annex wrote:
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?


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

Jul 22 '05 #16
* annex:

what are the prerequisites for a class to be a valid exception class


It must have a working and public copy constructor and destructor.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #17
>
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.
Jul 22 '05 #18
annex wrote:
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.

--
Salu2
Jul 22 '05 #19
annex wrote:
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.


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;
}
Jul 22 '05 #20
In message <40********@news.tm.net.my>, annex <an*****@hotpop.com>
writes
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.
Lots of answers, but nobody seems to have answered this directly.
Answer: derive both from a common base class and catch by reference:

class MyExBase {...}
class MyEx1: public MyExBase {...}
class MyEx2: public MyExBase {...}

try
{
doSomething();
}
catch (MyExBase &)
{
sameCode();
}

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


No. But catch(Exception&) will catch anything derived from Exception.

--
Richard Herring
Jul 22 '05 #21
"Richard Herring" <ju**@[127.0.0.1]> wrote in message
Lots of answers, but nobody seems to have answered this directly.
Answer: derive both from a common base class and catch by reference:
My response, the first one, answered everything as yours.
class MyExBase {...}
class MyEx1: public MyExBase {...}
class MyEx2: public MyExBase {...}

try
{
doSomething();
}
catch (MyExBase &)
{
sameCode();
}

2) what's the difference btw catch(...) and catch(Exception). will
catch(Exception) catch all unhandled exceptions also like does catch(...)
?
No. But catch(Exception&) will catch anything derived from Exception.


Jul 22 '05 #22
In message <yN*********************@bgtnsc05-news.ops.worldnet.att.net>,
Siemel Naran <Si*********@REMOVE.att.net> writes
"Richard Herring" <ju**@[127.0.0.1]> wrote in message
Lots of answers, but nobody seems to have answered this directly.
Answer: derive both from a common base class and catch by reference:


My response, the first one, answered everything as yours.


Sorry, must have missed it.
--
Richard Herring
Jul 22 '05 #23
Thanks all ;-)
"Richard Herring" <ju**@[127.0.0.1]> wrote in message
news:8z**************@baesystems.com...
In message <yN*********************@bgtnsc05-news.ops.worldnet.att.net>,
Siemel Naran <Si*********@REMOVE.att.net> writes
"Richard Herring" <ju**@[127.0.0.1]> wrote in message
Lots of answers, but nobody seems to have answered this directly.
Answer: derive both from a common base class and catch by reference:


My response, the first one, answered everything as yours.


Sorry, must have missed it.
--
Richard Herring

Jul 22 '05 #24
On 5 Jun 2004 20:14:13 GMT, Rob Williscroft <rt*@freenet.co.uk> wrote:

/* 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).


I "re-invented" the idiom when interfacing with Java via JNI. You
can't let C++ exceptions propogate into Java for obvious reasons (BOOM
goes the VM), and the above allowed me to have a single handler to map
C++ exceptions to Java ones.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #25

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
28
by: Frank Puck | last post by:
Meanwhile there are at least 8 years that compilers exist, which provide a working implementation of C++ Exception Handling. Has anything changed meanwhile? From my point of view nothing has...
9
by: C# Learner | last post by:
Some time ago, I remember reading a discussion about the strengths and weaknesses of exception handling. One of the weaknesses that was put forward was that exception handling is inefficient (in...
6
by: Jesper Ordrup Christensen | last post by:
Say I've created a piece of code that involves both sql, io and some number conversions. Being a responsible developer I have tried to catch all of the exceptions - but how can I be sure? Is...
34
by: rawCoder | last post by:
I have read that Exception Handling is expensive performance wise ( other than Throw ) , but exactly how ? Please consider the following example ... ////////////////// Code Block 1...
2
by: Rajeev Soni | last post by:
Hi, Considering the scenario for handling exceptions in Web Application where we have Presentation layer, Business layer and Data Access layer; if there any exception is occurred in DAL, what is...
16
by: Chuck Cobb | last post by:
I'm implementing a centralized exception handling routine using the Enterprise Library Exception Management Application Block. I trap all unhandled exceptions to one place using the following...
5
by: Bry | last post by:
I've created a class that offers an enhanced way of handling fatal exceptions. The class allows the user to optionaly submit a http based anonymous error report to myself, and also records details...
35
by: jeffc226 | last post by:
I'm interested in an idiom for handling errors in functions without using traditional nested ifs, because I think that can be very awkward and difficult to maintain, when the number of error checks...
35
by: eliben | last post by:
Python provides a quite good and feature-complete exception handling mechanism for its programmers. This is good. But exceptions, like any complex construct, are difficult to use correctly,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.