473,320 Members | 1,922 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,320 software developers and data experts.

Moving from Return Codes to Exception Handling

I keep trying to get myself out of the return-code mindset, but it
doesn't seem to work. They are suppose to get rid of if-then statements
of return codes, but you still have to do an if statement once the code
leaves your control (3rd party libraries, OS functions, etc) to throw
an exception?

Does anyone have any good suggestions on a book or website that deals
with exception handling that could help turn this poor mind off of
return codes? =)

Thanks,
Josh McFarlane

Jul 23 '05 #1
6 1876
Josh Mcfarlane wrote:
I keep trying to get myself out of the return-code mindset, but it
doesn't seem to work. They are suppose to get rid of if-then statements
of return codes, but you still have to do an if statement once the code
leaves your control (3rd party libraries, OS functions, etc) to throw
an exception?
Yes, if you want to raise an exception for every 3rd party library
call, but you don't have to. The point of exceptions is not to get rid
of error handling altogether, just make it easier/more natural to pass
information about the error from the point where the error occurs to
the point where the error recovery occurs. In other words, you don't
need to do error checks at all levels in the caller tree and you don't
have to use special error reporting functions or globals such as
'errno'. All the information about the error is contained in an
exception object.

You also have the added advantage that you don't need to write code
like this:

if(func1())
{
if(!func2())
undofunc1();
else
{
if(func3())
{
undofunc2();
undofunc1();
}
else
...
}
}

Or use goto statements to do the above. Each of the above operations
can be done in a class constructor and undone in a class destructor.
You would only have to to write:

Operation1 op1(/*params here*/);
Operation2 op2(/*params here*/);
Operation3 op3(/*params here*/);

Much cleaner isn't it?
Does anyone have any good suggestions on a book or website that deals
with exception handling that could help turn this poor mind off of
return codes? =)


"Exceptional C++", by Herb Sutter.
Bart.

Jul 23 '05 #2
On 2005-07-12, Josh Mcfarlane <da*****@gmail.com> wrote:
I keep trying to get myself out of the return-code mindset, but it
doesn't seem to work. They are suppose to get rid of if-then statements
of return codes, but you still have to do an if statement once the code
leaves your control (3rd party libraries, OS functions, etc) to throw
an exception?


One way is a layer of indirection -- just wrap the functions.

This is not always a satisfactory solution, but there are instances where it's
exactly what you need.

Another way you could do this is to wrap the exception constructor:

void FileErrorCheck (FILE* f)
{
if (f == NULL) { throw FileError ( errno ); }
}

....

FILE* f;
FileErrorCheck( f = fopen(filename,"r"));

Of course you still have to type some extra code per call, but the bottom line
is that there's got to be extra code somewhere -- either in the calling function
or the called function (via a wrapper)

Code similar to the above example is sometimes used by C programmers -- they'll
have a macro that does a bunch of things to reduce the code overhead associated
with this sort of checking.

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 23 '05 #3
Josh Mcfarlane wrote:
I keep trying to get myself out of the return-code mindset, but it
doesn't seem to work. They are suppose to get rid of if-then statements
of return codes, but you still have to do an if statement once the code
leaves your control (3rd party libraries, OS functions, etc) to throw
an exception?

Does anyone have any good suggestions on a book or website that deals
with exception handling that could help turn this poor mind off of
return codes? =)

Thanks,
Josh McFarlane


Like, get with the '90s dude! ;)

Exception handling in C++ has been one of the hardest adjustments for me to
make while coming from Java to C++. It's hard to use exceptions well in
C++; to a large extent because the other guy doesn't do it well,
consistently, or at all. In addition, there is much more flexibility (than
in Java) in what is actually thrown, how it is caught (by reference, or by
value), how it is (or can be) processed, and in using exception
specifications.

As for a source on exception handling, there is Stroustrup's tome,
TC++PL(SE). It is exhaustive in its discussion of exception handling.
That's not exactly the same as saying it is helpful.

For me, one consistent difficulty is to understand what the various types of
std::exception derivatives really mean. For instance. what are the
differences between std::out_of_range, std::range_error and
std::length_error? Sure, I can look it up when I need to know, but I
rarely remember the subtleties when I want to use one of these. That makes
using them time consuming.

--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #4
Josh Mcfarlane wrote:
I keep trying to get myself out of the return-code mindset, but it
doesn't seem to work. They are suppose to get rid of if-then statements
of return codes, but you still have to do an if statement once the code
leaves your control (3rd party libraries, OS functions, etc) to throw
an exception?


Just write a wrapper function with the if-throw, and use the wrapper instead
or the original function.

--
Salu2
Jul 23 '05 #5
> I keep trying to get myself out of the return-code mindset, but it
doesn't seem to work. They are suppose to get rid of if-then statements
of return codes, but you still have to do an if statement once the code
leaves your control (3rd party libraries, OS functions, etc) to throw
an exception?
You always have to adapt to old code or 3rd party libraries so that's
nothing new. Exceptions were not meant to "get rid of if-then
statements of return codes", but to signal to the caller that something
happened and recovery is impossible. They are many cases where error
codes just don't make sense, such as when every possible return value
is legal or in a constructor. Error codes are still very useful.
Does anyone have any good suggestions on a book or website that deals
with exception handling that could help turn this poor mind off of
return codes? =)


Concerning return codes and exceptions no, but Herb Sutter wrote two
good books about (among other things) exception handling ( [More]
Exceptional C++).

Make sure you don't go crazy with exceptions. Use them when 1) they
make the code clearer; 2) you cannot use return values; 3) few other
circumstances.

Jonathan

Jul 23 '05 #6
Donovan Rebbechi wrote:
void FileErrorCheck (FILE* f)
{
if (f == NULL) { throw FileError ( errno ); }
}

...

FILE* f;
FileErrorCheck( f = fopen(filename,"r"));


That doesn't work if you have multiple operations and you need to undo
the effects of previous operations before throwing. To do that a better
way is to wrap the calls in classes as in the example I have given.
Bart.

Jul 23 '05 #7

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

Similar topics

2
by: Josh Close | last post by:
Does anyone know what the return codes for an mx adns python lookup are? I know 0 means a valid domain, and anything else isn't, but there are "no nameservers found" and "timeout" and other things...
11
by: adi | last post by:
Dear all, This is more like a theoretical or conceptual question: which is better, using exception or return code for a .NET component? I had created a COM object (using VB6), which uses...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
1
by: chen | last post by:
We're having an internal debate about the merits & demerits of returning status codes in the output message vs exceptions to signify errors in handling a Web method. The status code camp is...
40
by: Mark P | last post by:
I'm implementing an algorithm and the computational flow is a somewhat deep. That is, fcn A makes many calls to fcn B which makes many calls to fcn C, and so on. The return value of the outermost...
24
by: Earl | last post by:
I have all of my data operations in a separate library, so I'm looking for what might be termed "best practices" on a return type from those classes. For example, let's say I send an update from...
5
by: Fir5tSight | last post by:
Hi All, I have a C#.NET code as follows: private void ScanInput_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { try { Row lRow = this.Connection.InsertScannedFile(ID);
7
by: =?Utf-8?B?TW9iaWxlTWFu?= | last post by:
Hello everyone: I am looking for everyone's thoughts on moving large amounts (actually, not very large, but large enough that I'm throwing exceptions using the default configurations). We're...
11
by: Jim in Arizona | last post by:
I've looked around the web but can't find anything to help me out. I was able to get some code to move some files from one directory to another, which works fine: ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.