473,386 Members | 2,050 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,386 software developers and data experts.

Error Managment

Hi,

If I have a long boolean function that has regular error checking
throughout, and simply returns false upon an error, whats the best
approach to getting detailed information about the error in the code
segment that called the function?

For example in a function:
{
....
open filea
check(filea open failed)
return false
open fileb
check(fileb open failed)
return false
....
}

How could i alter this so that the code that calls the above function
can tell if filea or fileb failed with a detailed error message for
the user?

My theoretical approach, which might be wrong is as follows:

I would create a public variable in my class : string LAST_ERROR;

then set this with detailed information right before 'return false',
then the calling segment could read the contents of last error, and
echo it to the user.

Is this the best approach to error handling? I have heard about using
error codes, but i cant think how i this could be implemented, or if
there is a more standardized method.

Thanks for any advice,

Jack

Aug 3 '07 #1
4 1400
On 2 ago, 21:41, JackC <jeche...@gmail.comwrote:
Hi,

If I have a long boolean function that has regular error checking
throughout, and simply returns false upon an error, whats the best
approach to getting detailed information about the error in the code
segment that called the function?

For example in a function:
{
...
open filea
check(filea open failed)
return false
open fileb
check(fileb open failed)
return false
...

}

How could i alter this so that the code that calls the above function
can tell if filea or fileb failed with a detailed error message for
the user?

My theoretical approach, which might be wrong is as follows:

I would create a public variable in my class : string LAST_ERROR;

then set this with detailed information right before 'return false',
then the calling segment could read the contents of last error, and
echo it to the user.

Is this the best approach to error handling? I have heard about using
error codes, but i cant think how i this could be implemented, or if
there is a more standardized method.

Thanks for any advice,

Jack

Throw an exception. In the following example. If you have a file named
test in the directory you run the executable obtained by compiling the
program below you get:

Path 2 invalid!

The code:

#include <fstream>
#include <iostream>

class ExceptionA {};
class ExceptionB {};
bool myFun( const char* path1, const char* path2 )
{
std::ifstream fileA( path1 );
if ( !fileA.good() ) throw( ExceptionA() );

std::ifstream fileB(path2);
if ( !fileB.good() ) throw( ExceptionB() );
}

int main( int argc, char* argv[], char* env[] )
{
try
{
myFun( "test", "wrong path" );
}
catch ( const ExceptionA& e )
{
std::cerr << "Path 1 invalid!\n";
}
catch ( const ExceptionB& e )
{
std::cerr << "Path 2 invalid!\n";
}

return( 0 );
}

Notice that you do not actually have to create a class for every
possible exception, but, instead, you may include lots of information
in the thrown object with an appropriate constructor. You can then use
it via the "e" reference caught by the catch clause. There is much
more to that, a good source (not only in this subject) is:

http://www.parashift.com/c++-faq-lite/exceptions.html

Elias Salomão Helou Neto.

Aug 3 '07 #2
Sorry, but the above code was somewhat sloppy in that it did not
return any value from the myFun function if it succeeds. Even though
the program compiles and runs, this would better be corrected.

Notice that when throwing, however, the return statement would make no
sense anyway.

Aug 3 '07 #3
On Aug 3, 2:41 am, JackC <jeche...@gmail.comwrote:
If I have a long boolean function
You have a problem. Functions shouldn't be long.
that has regular error checking
throughout, and simply returns false upon an error,
Not a very good choice, I would think. A boolean says true or
false; it answers a yes or no question. ("isEmpty()", for
example.) It doesn't say succeeded or failed, much less why
something failed.
whats the best approach to getting detailed information about
the error in the code segment that called the function?
Returning the information in the return value.
For example in a function:
{
...
open filea
check(filea open failed)
return false
open fileb
check(fileb open failed)
return false
...
}
How could i alter this so that the code that calls the above function
can tell if filea or fileb failed with a detailed error message for
the user?
I doubt that you want a detailed error message at this point;
just the necessary information so that higher level code can
formulate the message. (At this level, you probably don't want
to be concerned with issues of internationalization, for
example.)
My theoretical approach, which might be wrong is as follows:
I would create a public variable in my class : string LAST_ERROR;
then set this with detailed information right before 'return false',
then the calling segment could read the contents of last error, and
echo it to the user.
Sounds horrible. Just because Unix and Windows have adopted
more or less this approach (errno, GetLastError) doesn't mean
that it's a good idea.
Is this the best approach to error handling? I have heard
about using error codes, but i cant think how i this could be
implemented, or if there is a more standardized method.
The usual situation, when there is a need only to report an
error, and no additional need for information in the absense of
an error, is tu use a return code. Just define an enum with OK
andthe possible errors, and return it. Or wrap it in a class,
which will abort if the destructor is called without the error
having been checked.

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

Aug 3 '07 #4
"JackC" <je******@gmail.comwrote in message
news:11**********************@e9g2000prf.googlegro ups.com...
Hi,

If I have a long boolean function that has regular error checking
throughout, and simply returns false upon an error, whats the best
approach to getting detailed information about the error in the code
segment that called the function?

For example in a function:
{
...
open filea
check(filea open failed)
return false
open fileb
check(fileb open failed)
return false
...
}

How could i alter this so that the code that calls the above function
can tell if filea or fileb failed with a detailed error message for
the user?

My theoretical approach, which might be wrong is as follows:

I would create a public variable in my class : string LAST_ERROR;

then set this with detailed information right before 'return false',
then the calling segment could read the contents of last error, and
echo it to the user.

Is this the best approach to error handling? I have heard about using
error codes, but i cant think how i this could be implemented, or if
there is a more standardized method.
As Elas showed, you should throw an exception, although you should inherit
from std::exception. See FAQ 17.6
http://www.parashift.com/c++-faq-lit....html#faq-17.6

The output of the following program is:
Path1 bad

#include <stdexcept>
#include <fstream>
#include <iostream>

class MyException : public std::runtime_error
{
public:
MyException(const std::string& What ): std::runtime_error(What) { }
};

bool myFun( const char* path1, const char* path2 )
{
std::ifstream fileA( path1 );
if ( !fileA.good() )
throw( MyException("Path1 bad") );

std::ifstream fileB(path2);
if ( !fileB.good() )
throw( MyException("Path2 bad") );

return true;
}

int main()
{
try
{
myFun( "Foo.txt", "Bar.txt" );
}
catch ( std::exception& e )
{
std::cout << e.what();
}
}

I took the exception and modified it from the FAQ. I'm not sure, but you may
be ablet o simply throw std::exception( What ); I don't know, didnt' test
it. Excercise for the reader.
Aug 3 '07 #5

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

Similar topics

1
by: D. Buck | last post by:
Details: MS SQL 2000 dual Intel 1.2 GHz processors. 1 GB RAM 2.1 GB dB Dynamic Memory Managment. No other apps running on this server. First question: Since I have Dynamic Memory Managment...
4
by: Jorge_Beteta | last post by:
Hello, I work for an attorney staff, so basically the whole day they are sending themselves a lot and a lot of papers (Word docs or Excell sheets). We are going to propose them the use of a...
0
by: falcon | last post by:
I know I came after the battle. And I have just another sight on context managment. Simple Context Managment may look in Python 2.4.1 like this: Synhronized example: def...
1
by: vpr | last post by:
Hi This is a noob question, but here goes. I have a class that calls a function. class test: def __init__(self): if foo(): print "it worked"
2
by: Jason Davis | last post by:
Hi there, Is anyone familier with an access managment system for orders? Something simple like products, alphabetic list of clients and archive of ordered products. Thanks! Jas.
4
by: Jason Jacob | last post by:
To all, I've tried to use System.IO.File.Move (srcFile, desFile); but there is an exception similar to this : The process can't access desFile because it's being used by another process I...
3
by: Eirik Eldorsen | last post by:
I'm starting a project where I willl need an Ad Managment solution. Could someone recommend me a product? -- Eirik
2
by: Daniel | last post by:
Hi, I have a question regarding the memory managment in stl stacks. I want to use stacks to store a very large amount of numbers (some millions), thus I'm interested in how the stack behaves...
1
by: kalid2002 | last post by:
I have treid alot to do this program Library managment system by java Contiguous Array but i fount alot of defficulties so i hope any one could help me
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...

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.