473,569 Members | 3,009 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is this a good idea? (Error handling with overloaded operators)

Hi.

Is this a good idea?:

<begin code>
/* Addition operator: += */
const BigFix &BigFix::operat or+=(const BigFix &rhs)
{
ErrorType err;
int lhs_sign = sign, rhs_sign = rhs.sign;

/* Special cases */
if(lhs_sign == 0) /* interpreted as zero */
{
/* treat as zero */
return(rhs);
}

if(rhs_sign == 0) /* interpreted as zero */
{
/* treat as zero */
return(*this);
}

/* Compare signs */
if(lhs_sign == rhs_sign)
{
/* Just do ordinary addition */
if((err = MPFix_AddUnsign ed(this, &rhs)).dwErrCod e !=
XXXX_SUCCESS)
throw Exception(err); <<<<<<<<<<<<<<< <<< here

/* Set sign */
sign = lhs_sign;
}

if((lhs_sign == 1) && (rhs_sign == -1))
{
/* Subtract */
if((err = MPFix_SubUnsign ed(this, &rhs)).dwErrCod e !=
XXXX_SUCCESS)
throw Exception(err); <<<<<<<<<<<<<<< <<< here
}

if((lhs_sign == -1) && (rhs_sign == 1))
{
/* Subtract */
sign = 1;
if((err = MPFix_SubUnsign ed(this, &rhs)).dwErrCod e !=
XXXX_SUCCESS)
throw Exception(err); <<<<<<<<<<<<<<< <<< here

/* Reverse sign (we subtracted |this| - |rhs|, we want |rhs| - |
this|) */
sign = -sign;
}

/* Done! */
return(*this);
}
<end code>

What does that do, you might ask? Well, it's for a bignum library I've
been making for a program that needs it, and it's supposed to overload
the "+=" operator to add the big numbers, in this case big fixed-point
numbers. I left out a few things like the definition of ErrorType and
MPFix_AddUnsign ed(), MPFix_SubUnsign ed() for brevity but they are not
what I am asking about here. You should be able to get the gist of
what the code is supposed to do, just remember that
MPFix_AddUnsign ed() and MPFix_SubUnsign ed() are just add/sub routines
that treat both operands as positive.

Anyway, with the explanation out of the way, I'd like some criticism
of this, especially of the whole error handling. Is it really a good
idea to just throw exceptions like that out of the operator on an
overflow? Or would it be better to instead have some sort of "error
flag" in the BigFix that is usually zero, but then is set to some
nonzero number when an error occurs, depending on the error? The
problem is that we have to catch exceptions from _every_ piece of
code that uses the numbers. Unfortunately (or fortunately?) these seem
to be the only two ways of getting error information outside of an
overloaded operator like that (since it's meant to be used in
expressions like "a += b" then it must return a _BigFix_ and not
something else). So is this a good idea or a bad one?

May 25 '07 #1
5 1692
mike3 wrote:
Is this a good idea?:
Anyway, with the explanation out of the way, I'd like some criticism
of this, especially of the whole error handling. Is it really a good
idea to just throw exceptions like that out of the operator on an
overflow? Or would it be better to instead have some sort of "error
flag" in the BigFix that is usually zero, but then is set to some
nonzero number when an error occurs, depending on the error?
It depends on the intended use. If the goal is behave as do the
built-in types, then aborting is probably the best solution; the
user should check his values up front. In many cases, however,
this is a bit brutal, and exceptions are a good compromise.
(Overflow will usually occur because the program didn't---or
couldn't---correctly check its input, not because program state
has been corrupted.) A flag or a special value which propagates
emulates IEEE behavior, of course, which has also proven itself
in practice. It does mean that you have to define behavior for
all such cases (but you can inspire yourself from the IEEE
specification).
The problem is that we have to catch exceptions from _every_
piece of code that uses the numbers.
If you don't catch it, you abort. Depending on use, that might
be correct.

--
James Kanze (Gabi Software) email: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 26 '07 #2
On May 26, 4:14 am, James Kanze <james.ka...@gm ail.comwrote:
mike3 wrote:
Is this a good idea?:
Anyway, with the explanation out of the way, I'd like some criticism
of this, especially of the whole error handling. Is it really a good
idea to just throw exceptions like that out of the operator on an
overflow? Or would it be better to instead have some sort of "error
flag" in the BigFix that is usually zero, but then is set to some
nonzero number when an error occurs, depending on the error?

It depends on the intended use. If the goal is behave as do the
built-in types, then aborting is probably the best solution; the
user should check his values up front. In many cases, however,
this is a bit brutal, and exceptions are a good compromise.
(Overflow will usually occur because the program didn't---or
couldn't---correctly check its input, not because program state
has been corrupted.) A flag or a special value which propagates
emulates IEEE behavior, of course, which has also proven itself
in practice. It does mean that you have to define behavior for
all such cases (but you can inspire yourself from the IEEE
specification).
The problem is that we have to catch exceptions from _every_
piece of code that uses the numbers.

If you don't catch it, you abort. Depending on use, that might
be correct.
However what if I don't want it to just abort because I neglected
to handle some exception in some loop or something that involves
use of the big number operations?
--
James Kanze (Gabi Software) email: james.ka...@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 26 '07 #3
On May 26, 4:14 am, James Kanze <james.ka...@gm ail.comwrote:
mike3 wrote:
Is this a good idea?:
Anyway, with the explanation out of the way, I'd like some criticism
of this, especially of the whole error handling. Is it really a good
idea to just throw exceptions like that out of the operator on an
overflow? Or would it be better to instead have some sort of "error
flag" in the BigFix that is usually zero, but then is set to some
nonzero number when an error occurs, depending on the error?

It depends on the intended use. If the goal is behave as do the
built-in types, then aborting is probably the best solution; the
user should check his values up front. In many cases, however,
this is a bit brutal, and exceptions are a good compromise.
(Overflow will usually occur because the program didn't---or
couldn't---correctly check its input, not because program state
has been corrupted.) A flag or a special value which propagates
emulates IEEE behavior, of course, which has also proven itself
in practice. It does mean that you have to define behavior for
all such cases (but you can inspire yourself from the IEEE
specification).
The problem is that we have to catch exceptions from _every_
piece of code that uses the numbers.

If you don't catch it, you abort. Depending on use, that might
be correct.
However what if I don't want it to just abort because I neglected
to handle some exception in some loop or something that involves
use of the big number operations?
--
James Kanze (Gabi Software) email: james.ka...@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 26 '07 #4
On 26 Maj, 21:47, mike3 <mike4...@yahoo .comwrote:
On May 26, 4:14 am, James Kanze <james.ka...@gm ail.comwrote:


mike3 wrote:
Is this a good idea?:
Anyway, with the explanation out of the way, I'd like some criticism
of this, especially of the whole error handling. Is it really a good
idea to just throw exceptions like that out of the operator on an
overflow? Or would it be better to instead have some sort of "error
flag" in the BigFix that is usually zero, but then is set to some
nonzero number when an error occurs, depending on the error?
It depends on the intended use. If the goal is behave as do the
built-in types, then aborting is probably the best solution; the
user should check his values up front. In many cases, however,
this is a bit brutal, and exceptions are a good compromise.
(Overflow will usually occur because the program didn't---or
couldn't---correctly check its input, not because program state
has been corrupted.) A flag or a special value which propagates
emulates IEEE behavior, of course, which has also proven itself
in practice. It does mean that you have to define behavior for
all such cases (but you can inspire yourself from the IEEE
specification).
The problem is that we have to catch exceptions from _every_
piece of code that uses the numbers.
No you don't: you catch the exception everywhere you want to handle
the error, and that is far rarer than every time you use your bignum.
>
If you don't catch it, you abort. Depending on use, that might
be correct.

However what if I don't want it to just abort because I neglected
to handle some exception in some loop or something that involves
use of the big number operations?
What is the alternative? I hope you don't want to just compute along
with bad data. The simple truth is that if there is an error, the best
way to handle it is to react to it. Ignoring it is not really an
option. Using exceptions requires you to do an active effort to
neglect it.
As James pointed out, using the IEEE specification could be a good
inspiration. One thing you could reasonably do is to specify the
action to take in case of an overflow. One reasonable approach could
be to return bigint_max or something like that, and if you specifiy
this overflow is no longer an error.

/Peter

May 26 '07 #5
On May 26, 9:47 pm, mike3 <mike4...@yahoo .comwrote:
On May 26, 4:14 am, James Kanze <james.ka...@gm ail.comwrote:
mike3 wrote:
Is this a good idea?:
Anyway, with the explanation out of the way, I'd like some criticism
of this, especially of the whole error handling. Is it really a good
idea to just throw exceptions like that out of the operator on an
overflow? Or would it be better to instead have some sort of "error
flag" in the BigFix that is usually zero, but then is set to some
nonzero number when an error occurs, depending on the error?
It depends on the intended use. If the goal is behave as do the
built-in types, then aborting is probably the best solution; the
user should check his values up front. In many cases, however,
this is a bit brutal, and exceptions are a good compromise.
(Overflow will usually occur because the program didn't---or
couldn't---correctly check its input, not because program state
has been corrupted.) A flag or a special value which propagates
emulates IEEE behavior, of course, which has also proven itself
in practice. It does mean that you have to define behavior for
all such cases (but you can inspire yourself from the IEEE
specification).
The problem is that we have to catch exceptions from _every_
piece of code that uses the numbers.
If you don't catch it, you abort. Depending on use, that might
be correct.
However what if I don't want it to just abort because I neglected
to handle some exception in some loop or something that involves
use of the big number operations?
Then catch the exception or use some other technique. As I
said, IEEE has had some success with using special values which
propagate.

--
James Kanze (Gabi Software) email: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 27 '07 #6

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

Similar topics

9
2281
by: John Cho | last post by:
// CHO, JOHN #include<iostream> class fracpri{ int whole; int numer; int denom;
20
37354
by: Brad Eck | last post by:
"The only operators that cannot be overloaded are :: (scope resolution), . (member selection), and .* (member selection through pointer to function). Quoting from Stroustrup's 3rd edition of _The C++ Programming Language_, section 11.2 (page 263), these three operators 'take a name, rather than a value, as their second operand and provide...
1
2214
by: masood.iqbal | last post by:
I have a few questions regarding overloaded typecast operators and copy constructors that I would like an answer for. Thanks in advance. Masood (1) In some examples that I have seen pertaining to casting class A to class B, the implementation of the
4
1604
by: masood.iqbal | last post by:
Please help me with this doubt that I have regarding overloaded operators. Sometimes they are member functions and sometimes they are friends (e.g. see the code snippet from Stroustrup, Second Edition that I have posted to comp.sources.d). How do we decide which is more appropriate? Why are the overloaded "<<" and ">>" operators always...
10
1969
by: maadhuu | last post by:
hi i wasnt to know the answer for the following. now ,u can overload all the operators which are basically determined at runtime (coz' of whch operators like sizeof())cannot be overloaded. now my doubt is , if u have something like p->a ....where p(say) is a pointer of a user defined type, then u can overload this because p is determined...
10
3197
by: nimmi_srivastav | last post by:
Below you will see an example of a nested conditional expression that this colleague of mine loves. He claims that it is more efficient that a multi-level if-else-if structure. Moreover, our complexity analyzer tool supposedly does not pick it up. Is it really more efficient? Personally I find this coding style extremely cryptic, misleading...
3
1733
by: iluvatar | last post by:
Hi all. I have written a 3d-vector class (for 3-dimensional space) and I have overloaded the arihtmetic operators like +, +=, * and so on. Also, the constructor works with doubles and has default arguments. In my class, the operator = works for another vector (just copying the elements), and for a double: in this cases each element of the...
159
6969
by: bernard | last post by:
howdy! please recommend a good c compiler. - should be small - should be fast - should come with a good ide - should be inexpensive i am using windows os.
23
3120
by: tonytech08 | last post by:
What I like about the C++ object model: that the data portion of the class IS the object (dereferencing an object gets you the data of a POD object). What I don't like about the C++ object model: that most OO features are not available for class object design without loss of POD-ness. So, I'm more than leaning toward "bad" because of...
0
7703
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...
0
7619
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7681
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...
0
7983
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...
1
5514
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...
0
5228
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...
0
3662
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...
0
3651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.