473,490 Members | 2,495 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Should exceptions be thrown by value or reference?

Is it better/more efficient to throw exceptions by value, as in

void func() throw (SomeException)
{
// do some stuff
throw SomeException("error message");
}

void bar()
{
try
{
foo()
}
catch (SomeException e)
{
// do some stuff
}
}

or by reference, as in

void func() throw (SomeException*)
{
// do some stuff
throw new SomeException("error message");
}

void bar()
{
try
{
foo()
}
catch (SomeException* e)
{
// do some stuff
delete e;
}
}

? Either method requires allocating memory (on the stack in the first
case, on the heap in the second), so I don't think that either is any
less safe than the other. Obviously, the method of throwing pointers is
prone to memory leaks if programmers are not careful, but is it any more
or less efficient?

Rennie
Jul 23 '05 #1
3 1865
Rennie deGraaf wrote:

Is it better/more efficient to throw exceptions by value, as in

void func() throw (SomeException)
{
// do some stuff
throw SomeException("error message");
}

void bar()
{
try
{
foo()
}
catch (SomeException e)
{
// do some stuff
}
}

or by reference, as in

void func() throw (SomeException*)
{
// do some stuff
throw new SomeException("error message");
}

void bar()
{
try
{
foo()
}
catch (SomeException* e)
{
// do some stuff
delete e;
}
}

or

void func() throw (SomeException)
{
// do some stuff
throw SomeException("error message");
}

void bar()
{
try
{
func();
}

catch (SomeException &re)
{
// do some stuff
}
}
? Either method requires allocating memory (on the stack in the first
case, on the heap in the second), so I don't think that either is any
less safe than the other. Obviously, the method of throwing pointers is
prone to memory leaks if programmers are not careful, but is it any more
or less efficient?

By reference (with references or pointers) is of course more space and
time efficient since no new exception object is created.
In non-GC systems, I would prefer references.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #2
"Rennie deGraaf" <ca.ucalgary.cpsc@degraaf> wrote in message
news:VZtUd.523356$6l.517910@pd7tw2no...
Is it better/more efficient to throw exceptions by value, as in

void func() throw (SomeException)
Read this before you use exception specifications:
http://www.gotw.ca/publications/mill22.htm
{
// do some stuff
throw SomeException("error message");
}
Otherwise this is OK, especially if SomeException is ultimately derived from
std::exception.

void bar()
{
try
{
foo()
}
catch (SomeException e)
{
// do some stuff
}
}
This is OK but don't expect any sort of polymorphic behavior. If you derive
another class from SomeException it will get bit sliced down to a
SomeException unless you change your catch phrase to

catch (SomeException &e)

or by reference, as in

void func() throw (SomeException*)
{
// do some stuff
throw new SomeException("error message");
}
I can't imagine why anyone would do this.

void bar()
{
try
{
foo()
}
catch (SomeException* e)
{
// do some stuff
delete e;
}
}

? Either method requires allocating memory (on the stack in the first
case, on the heap in the second), so I don't think that either is any less
safe than the other. Obviously, the method of throwing pointers is prone
to memory leaks if programmers are not careful, but is it any more or less
efficient?

Rennie


I don't understand why everyone is so concerned about efficiency in a
situation (propogation of an error) which one would certainly hope would
constitute an extremely small portion of the run time.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 23 '05 #3
Cy Edmunds wrote:
I don't understand why everyone is so concerned about efficiency in a
situation (propogation of an error) which one would certainly hope would
constitute an extremely small portion of the run time.

Basically where exceptions "by reference" (references or pointers) make
sense are on severely run-time/space constrained systems, when exception
types have no public copy constructors, and in GC systems I guess - in
..NET in addition to the usual unmanaged ISO C++ exception styles that
were mentioned here, managed exceptions are created in the managed heap
and caught as managed pointers:
try
{
throw __gc new SomeException;
}
catch(SomeException *pe)
{
// ...
}

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #4

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

Similar topics

7
9232
by: Michael Andersson | last post by:
Hi! Does the use of exception handling induce a performance penalty during the execution of non exception handling code? Regards, /Michael
40
2983
by: Steve Juranich | last post by:
I know that this topic has the potential for blowing up in my face, but I can't help asking. I've been using Python since 1.5.1, so I'm not what you'd call a "n00b". I dutifully evangelize on the...
10
342
by: Eric | last post by:
I have been researching how exceptions and dynamic_cast work to determine if I will use either feature for a game-console application. I have a few pre-concieved notions and questions that I was...
3
3386
by: Robert Rotstein | last post by:
It appears that exception handling at the top-most level of a C# program, in the static void Main() method, differs depending on whether the program is run in debug mode or not. That is, code such...
21
2650
by: Jim Langston | last post by:
I'm sure this has been asked a few times, but I'm still not sure. I want to create a function to simplify getting a reference to a CMap in a map. This is what I do now in code: ...
2
1449
by: usenet | last post by:
I am trying to collect a comprehensive listing of standard C++ exceptions. This is what I have got so far (by going through the standard). Kindly let me know if I missed anything. Thanks,...
11
1716
by: Jonathan Wood | last post by:
I must be the only one who doesn't think exceptions are the greatest thing since bread. Consider the following code: id = Convert.ToInt32(Request.QueryString); First, is there an easy way...
2
1462
by: allen.fowler | last post by:
Hi, My code looks like this: for item in bigset: self.__sub1(item) self.__sub2(item) self.__sub3(item) # the subX functions, in turn, use various 3rd party modules.
8
2066
by: wrungel | last post by:
Exceptions thrown by C++ function which is called by a C-function which is called by a C++ function are not catched by outermost C++ function. Operating system: Linux Compiler: GNU GCC Version...
0
7112
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
6974
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
5448
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,...
1
4878
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...
0
4573
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...
0
3084
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...
0
3074
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
628
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.