473,811 Members | 3,356 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2277
"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

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

Similar topics

9
3209
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 indeed also true for our language of choice, Python. Its file type allows some extraordinary convenient access like: for line in open("blah"): handle_line(line)
28
2265
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 changed -- people are still using a coding style, which I call pre-C++-Exception-Handling. Is this different in your experience? What is your experience regarding this subject? I think that a C++ IOstream lib, which is by standard free of exceptions...
9
2542
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 the way of CPU usage), compared to the "normal" practise returning values. How true is this? Will using using exception handling, in general, be much less efficient than returning values, or less efficient at all? Just curious...
6
746
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 there a way to list exactly which exceptions that are not handlet? (design time) I could of cause ... use catch (Exception e) {...} as the final catch ... but I would prefer to now the "landscape" before doing that ..
34
2242
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 ////////////////////////////////// Try { for ( ... ){// Code that might throw exception} }catch(...){} //////////////////////////////////////////////////////////////////////////
2
5875
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 the best thing to do: 1. Dont catch the exception in DAL and let it prop up to the Application level and the Global.Application_Error event log it to any source and show let ASP.NET show custom error page provided in Web.Config file. OR 2....
16
2551
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 method: // --- Create an Exception Handler for Thread Exceptions ---------------- Application.ThreadException += new ThreadExceptionEventHandler(OnThreadException);
5
3833
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 in the application log. The static method is overloaded, and supports passing exceptions and/or strings just like throwing an exception.The class will also fall back to the standard exception handling if something goes wrong in my class. As an...
35
3812
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 gets about 3 or so. It also gets very awkward in nested loops, where you want to check for normal loop processing in the loop condition, not errors. Yes, you could put some generic exit flag in the loop condition, but when you're simply done if...
35
3524
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, especially as programs get large. Most of the issues of exceptions are not specific to Python, but I sometimes feel that Python makes them more acute because of the free-n- easy manner in which it employs exceptions for its own uses and allows users...
0
9722
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10644
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10393
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10124
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7664
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6882
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.