Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > handling exceptions

Reply
Thread Tools

handling exceptions

 
 
annex
Guest
Posts: n/a
 
      06-05-2004
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



 
Reply With Quote
 
 
 
 
Siemel Naran
Guest
Posts: n/a
 
      06-05-2004
"annex" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

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




 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      06-05-2004
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


 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      06-05-2004
* 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?
 
Reply With Quote
 
Roberto =?ISO-8859-15?Q?D=EDaz?=
Guest
Posts: n/a
 
      06-05-2004
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
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      06-05-2004
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?

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      06-05-2004
* 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?
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      06-05-2004
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?

 
Reply With Quote
 
Rob Williscroft
Guest
Posts: n/a
 
      06-05-2004
Rolf Magnus wrote in news:c9solc$njg$00$(E-Mail Removed)-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/
 
Reply With Quote
 
annex
Guest
Posts: n/a
 
      06-06-2004

"Rolf Magnus" <(E-Mail Removed)> wrote in message
news:c9s254$hr9$02$(E-Mail Removed)-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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exceptions - How do you make it work like built-in exceptions? Lie Python 3 01-14-2008 06:45 PM
Exceptions + Performance on path without exceptions gratch06@gmail.com C++ 3 04-16-2007 08:52 PM
Handling Exceptions in an n-tier environment Raterus ASP .Net 6 08-24-2004 08:21 PM
Checked exceptions vs unchecked exceptions Ahmed Moustafa Java 5 07-14-2004 01:46 PM
Custom exceptions -- inherit from exceptions.Exception? Paul Miller Python 3 11-12-2003 09:24 AM



Advertisments